in modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/CompositeConfigurationServiceImpl.java [216:487]
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Get the request path
String path = URLDecoder.decode(request.getRequestURI().substring(request.getServletPath().length()), "UTF-8");
String key;
if (path.startsWith("/")) {
if (path.length() > 1) {
key = path.substring(1);
} else {
key ="";
}
} else {
key =path;
}
logger.fine("get " + key);
// Expect a key in the form composite:contributionURI;namespace;localName or
// a path in the form componentName/componentName/...
// and return the corresponding resolved composite
String requestedContributionURI = null;
QName requestedCompositeName = null;
String[] requestedComponentPath = null;
if (key.startsWith("composite:")) {
// Extract the composite qname from the key
requestedContributionURI = contributionURI(key);
requestedCompositeName = compositeQName(key);
} else if (key.length() != 0) {
// Extract the path to the requested component from the key
requestedComponentPath = key.split("/");
}
// Somewhere to store the composite we expect to write out at the end
Composite requestedComposite = null;
// Create a domain composite model
Composite domainComposite = assemblyFactory.createComposite();
domainComposite.setName(new QName(Constants.SCA10_TUSCANY_NS, "domain"));
// Get the domain composite items
Entry<String, Item>[] domainEntries = domainCompositeCollection.getAll();
// Populate the domain composite
Workspace workspace = workspaceFactory.createWorkspace();
workspace.setModelResolver(new ExtensibleModelResolver(workspace, extensionPoints));
Map<String, Contribution> contributionMap = new HashMap<String, Contribution>();
for (Entry<String, Item> domainEntry: domainEntries) {
// Load the required contributions
String contributionURI = contributionURI(domainEntry.getKey());
Contribution contribution = contributionMap.get(contributionURI);
if (contribution == null) {
// The contribution has not been loaded yet, load it with all its dependencies
Entry<String, Item>[] entries = contributionCollection.query("alldependencies=" + contributionURI);
for (Entry<String, Item> entry: entries) {
Item dependencyItem = entry.getData();
String dependencyURI = entry.getKey();
if (!contributionMap.containsKey(dependencyURI)) {
// Read the contribution
Contribution dependency;
try {
String dependencyLocation = dependencyItem.getAlternate();
dependency = contribution(workspace, dependencyURI, dependencyLocation);
} catch (Exception e) {
if (contributionURI.equals(dependencyURI)) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, getDescription(e));
return;
} else {
continue;
}
}
workspace.getContributions().add(dependency);
contributionMap.put(dependencyURI, dependency);
if (contributionURI.equals(entry.getKey())) {
contribution = dependency;
}
}
}
}
if (contribution == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, contributionURI);
return;
}
// Find the specified deployable composite in the contribution
Composite deployable = null;
QName qn = compositeQName(domainEntry.getKey());
for (Composite d: contribution.getDeployables()) {
if (qn.equals(d.getName())) {
deployable = d;
break;
}
}
if (deployable == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, qn.toString());
return;
}
// add the deployable composite to the domain composite
domainComposite.getIncludes().add(deployable);
// Fuse includes into the deployable composite
try {
compositeIncludeBuilder.build(deployable);
analyzeProblems();
} catch (Exception e) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, getDescription(e));
return;
}
// Store away the requested composite
if (requestedCompositeName != null) {
if (requestedContributionURI.equals(contributionURI) && requestedCompositeName.equals(deployable.getName())){
requestedComposite = deployable;
}
}
}
// The requested composite was not found
if (requestedCompositeName != null && requestedComposite == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, key);
return;
}
// Get the clouds composite
Composite cloudsComposite;
try {
cloudsComposite = cloud();
} catch (NotFoundException e) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());
return;
}
// configure the endpoints for each composite in the domain
List<Composite> domainIncludes = domainComposite.getIncludes();
for (int i = 0, n =domainIncludes.size(); i < n; i++) {
Composite composite = domainIncludes.get(i);
QName compositeName = composite.getName();
String contributionURI = contributionURI(domainEntries[i].getKey());
// find the node that will run this composite and the default
// bindings that it configures
Component nodeComponent = null;
QName nodeCompositeName = null;
for (Composite cloudComposite : cloudsComposite.getIncludes()) {
for (Component nc : cloudComposite.getComponents()) {
NodeImplementation nodeImplementation = (NodeImplementation)nc.getImplementation();
if (nodeImplementation.getComposite().getName().equals(compositeName) &&
nodeImplementation.getComposite().getURI().equals(contributionURI)) {
nodeImplementation.setComposite(composite);
nodeComponent = nc;
nodeCompositeName = cloudComposite.getName();
break;
}
}
}
if (nodeComponent != null) {
try {
Composite nodeComposite = assemblyFactory.createComposite();
nodeComposite.setName(nodeCompositeName);
nodeComposite.getComponents().add(nodeComponent);
nodeConfigurationBuilder.build(nodeComposite);
} catch (CompositeBuilderException e) {
throw new ServletException(e);
}
}
}
// Build the domain composite
SCADefinitions aggregatedDefinitions = new SCADefinitionsImpl();
for (SCADefinitions definition : policyDefinitions) {
SCADefinitionsUtil.aggregateSCADefinitions(definition, aggregatedDefinitions);
}
CompositeBuilder compositeBuilder = new CompositeBuilderImpl(assemblyFactory, null, scaBindingFactory,
intentAttachPointTypeFactory, documentBuilderFactory, transformerFactory,
contractMapper, aggregatedDefinitions, monitor, bindingMap);
try {
compositeBuilder.build(domainComposite);
analyzeProblems();
} catch (Exception e) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, getDescription(e));
return;
}
// Return the requested composite
if (requestedComposite != null) {
// Rebuild the requested composite from the domain composite
// we have to reverse the flattening that went on when the domain
// composite was built
List<Component> tempComponentList = new ArrayList<Component>();
tempComponentList.addAll(requestedComposite.getComponents());
requestedComposite.getComponents().clear();
for (Component inputComponent : tempComponentList){
for (Component deployComponent : domainComposite.getComponents()){
if (deployComponent.getName().equals(inputComponent.getName())){
requestedComposite.getComponents().add(deployComponent);
}
}
}
} else if (requestedComponentPath != null) {
// If a component path was specified, walk the path to get to the requested
// component and the composite that implements it
Composite nestedComposite = domainComposite;
for (String componentName: requestedComponentPath) {
Component component = null;
for (Component c: nestedComposite.getComponents()) {
if (componentName.equals(c.getName())) {
component = c;
break;
}
}
if (component == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, key);
return;
} else {
if (component.getImplementation() instanceof Composite) {
nestedComposite = (Composite)component.getImplementation();
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND, key);
return;
}
}
}
// Return the nested composite
requestedComposite = nestedComposite;
} else {
// Return the whole domain composite
requestedComposite = domainComposite;
}
// Write the composite in the requested format
StAXArtifactProcessor<Composite> processor;
String queryString = request.getQueryString();
if (queryString != null && queryString.startsWith("format=")) {
String format = queryString.substring(7);
int s = format.indexOf(';');
QName formatName = new QName(format.substring(0, s), format.substring(s +1));
processor = (StAXArtifactProcessor<Composite>)staxProcessors.getProcessor(formatName);
if (processor == null) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, new IllegalArgumentException(queryString).toString());
return;
}
} else {
processor = compositeProcessor;
}
try {
response.setContentType("text/xml");
XMLStreamWriter writer = outputFactory.createXMLStreamWriter(response.getOutputStream());
processor.write(requestedComposite, writer);
} catch (Exception e) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
return;
}
}