private void manageProfile(TaskContext context) { Container current = fabricService.get().getCurrentContainer(); ProfileData profileData = createProfileData(context); String profileId = context.getConfiguration().get(TEMPLATE_PROFILE_PROPERTY_NAME) + "-" + name; Version version = current.getVersion(); try { if (lock.acquire(60, TimeUnit.SECONDS)) { if (profileData.isEmpty()) { if (version.hasProfile(profileId)) { // Just delete the profile version.getProfile(profileId).delete(true); } return; } else if (!version.hasProfile(profileId)) { // Create the profile fabricService.get().getDataStore().createProfile(version.getId(), profileId); } Profile managedProfile = version.getProfile(profileId); // managedProfile.setConfigurations(profileData.getConfigs()); managedProfile.setFileConfigurations(profileData.getFiles()); current.addProfiles(managedProfile); } else { throw new TimeoutException("Timed out waiting for lock"); } } catch (Exception e) { LOGGER.error("Error managing work items.", e); } finally { releaseLock(); } }
@Override public void stop(TaskContext context) { Container current = fabricService.get().getCurrentContainer(); Version version = current.getVersion(); String profileId = context.getConfiguration().get(TEMPLATE_PROFILE_PROPERTY_NAME) + "-" + name; if (version.hasProfile(profileId)) { // Just delete the profile version.getProfile(profileId).delete(true); } }
/** * Creates a representation of the profile based on the assigned item for the specified * {@linkTaskContext}. * * @param context * @return */ private ProfileData createProfileData(TaskContext context) { ProfileData profileData = new ProfileData(); Set<WorkItem> workItems = assignedWorkItems.get(context); if (workItems.isEmpty()) { return profileData; } Container current = fabricService.get().getCurrentContainer(); Version version = current.getVersion(); String templateProfileName = String.valueOf(context.getConfiguration().get(TEMPLATE_PROFILE_PROPERTY_NAME)); Profile templateProfile = version.getProfile(templateProfileName); Set<String> allFiles = templateProfile.getFileConfigurations().keySet(); Iterable<String> mvelFiles = Iterables.filter(allFiles, MvelPredicate.INSTANCE); Iterable<String> plainFiles = Iterables.filter(allFiles, Predicates.not(MvelPredicate.INSTANCE)); for (String mvelFile : mvelFiles) { Key key = new Key(templateProfile.getId(), mvelFile); synchronized (templates) { CompiledTemplate template = templates.get(key); if (template == null) { template = TemplateCompiler.compileTemplate( new String(templateProfile.getFileConfigurations().get(mvelFile)), parserContext); templates.put(key, template); } } } for (WorkItem workItem : workItems) { Map<String, WorkItem> data = new HashMap<String, WorkItem>(); data.put(WorkItem.ITEM, workItem); // Render templates for (String fileTemplate : mvelFiles) { String file = renderTemplateName(fileTemplate, workItem); Key key = new Key(templateProfile.getId(), fileTemplate); try { String renderedTemplate = TemplateRuntime.execute(templates.get(key), parserContext, data).toString(); updateProfileData(file, renderedTemplate, profileData); } catch (Exception ex) { LOGGER.warn("Failed to render {}. Ignoring.", fileTemplate); } } // Copy plain files. for (String file : plainFiles) { String content = new String(templateProfile.getFileConfigurations().get(file)); updateProfileData(file, content, profileData); } } return profileData; }
protected void recompile() { LOG.debug("Looking for XSDs to recompile"); Set<String> urls = new TreeSet<String>(); FabricService fabric = getFabricService(); Container container = fabric.getCurrentContainer(); String version = container.getVersion().getId(); List<Profile> profiles = Containers.overlayProfiles(container); List<String> profileIds = Profiles.profileIds(profiles); Collection<String> names = fabric.getDataStore().listFiles(version, profileIds, schemaPath); for (String name : names) { if (name.endsWith(".xsd")) { String prefix = schemaPath; if (Strings.isNotBlank(prefix)) { prefix += "/"; } urls.add("profile:" + prefix + name); } } LOG.info("Recompiling XSDs at URLs: " + urls); startedFlag.set(false); ClassLoader classLoader = AriesFrameworkUtil.getClassLoader(bundleContext.getBundle()); if (classLoader == null) { classLoader = getClass().getClassLoader(); } DynamicXJC xjc = new DynamicXJC(classLoader); xjc.setSchemaUrls(new ArrayList<String>(urls)); compileResults = xjc.compileSchemas(); if (handler != null) { handler.onCompileResults(compileResults); } if (introspector != null) { introspector.setClassLoaderProvider("dynamic.jaxb", new ClassLoaderProvider() { @Override public ClassLoader getClassLoader() { return compileResults.getClassLoader(); } }); } }
@Test public void testRegistryEntries() throws Exception { System.out.println(executeCommand("fabric:create -n root")); System.out.println(executeCommand("fabric:profile-list")); ServiceProxy<FabricService> fabricProxy = ServiceProxy.createServiceProxy(bundleContext, FabricService.class); try { FabricService fabricService = fabricProxy.getService(); CuratorFramework curator = fabricService.adapt(CuratorFramework.class); Set<ContainerProxy> containers = ContainerBuilder.create(fabricProxy, 3) .withName("fabric-camel") .withProfiles("feature-camel") .assertProvisioningResult() .build(); try { // We will use the first container as a client and the rest as servers. LinkedList<Container> containerList = new LinkedList<Container>(containers); Container client = containerList.removeLast(); LinkedList<Container> servers = new LinkedList<Container>(containerList); for (Container c : servers) { Profile p = c.getVersion().getProfile("example-camel-cluster.server"); c.setProfiles(new Profile[] {p}); } Provision.provisioningSuccess(servers, PROVISION_TIMEOUT); Profile p = client.getVersion().getProfile("example-camel-cluster.client"); client.setProfiles(new Profile[] {p}); Provision.provisioningSuccess(Arrays.asList(new Container[] {client}), PROVISION_TIMEOUT); System.out.println(executeCommand("fabric:container-list")); System.out.println(executeCommand("fabric:profile-display --overlay fabric-camel-server")); // Check that the entries have been properly propagated. Assert.assertNotNull(exists(curator, "/fabric/registry/camel/endpoints")); Assert.assertEquals(1, getChildren(curator, "/fabric/registry/camel/endpoints").size()); Assert.assertTrue( Provision.waitForCondition( Arrays.asList(client), new ContainerCondition() { @Override public Boolean checkConditionOnContainer(final Container c) { return getCompletedExchangesCount(c) > 0; } }, 60000L)); // We want to kill all but one server, so we take out the first and keep it to the end. Container lastActiveServerContainer = servers.removeLast(); for (Container c : servers) { try { c.destroy(true); } catch (Exception ex) { // ignore. } // Get the number of exchanges completed before we kill the server. final int completed = getCompletedExchangesCount(client); // Ensure that we still have messages flowing Assert.assertTrue( Provision.waitForCondition( Arrays.asList(client), new ContainerCondition() { @Override public Boolean checkConditionOnContainer(final Container c) { return getCompletedExchangesCount(c) > completed + 3; } }, 60000L)); } System.out.println( new AnsiString( executeCommand( "fabric:container-connect -u admin -p admin " + client.getId() + " camel:route-info fabric-client")) .getPlain() .toString()); } finally { ContainerBuilder.destroy(containers); } } finally { fabricProxy.close(); } }