diff options
Diffstat (limited to 'sca-java-2.x/tags/2.0-Beta4-RC1/modules/node-manager/src/main/java')
7 files changed, 550 insertions, 0 deletions
diff --git a/sca-java-2.x/tags/2.0-Beta4-RC1/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/DomainAssetManagerResource.java b/sca-java-2.x/tags/2.0-Beta4-RC1/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/DomainAssetManagerResource.java new file mode 100644 index 0000000000..bb017a5d5b --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta4-RC1/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/DomainAssetManagerResource.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.node.manager; + +import java.util.List; + +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; + +import org.oasisopen.sca.annotation.Remotable; + +@Remotable +public interface DomainAssetManagerResource { + + @GET + @Path("{domainURI}/resources/status") + List<Status> getResourceStatus(@PathParam("domainURI") @DefaultValue("default") String domainURI); + + + @GET + @Path("{domainURI}/services/status") + List<Status> getServiceStatus(@PathParam("domainURI") @DefaultValue("default") String domainURI); + +} diff --git a/sca-java-2.x/tags/2.0-Beta4-RC1/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/DomainCompositeResource.java b/sca-java-2.x/tags/2.0-Beta4-RC1/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/DomainCompositeResource.java new file mode 100644 index 0000000000..d9abd288ea --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta4-RC1/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/DomainCompositeResource.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.node.manager; + +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; + +import org.oasisopen.sca.annotation.Remotable; + +@Remotable +public interface DomainCompositeResource { + + @GET + @Path("{domainURI}") + String getDomainComposite(@PathParam("domainURI") @DefaultValue("default") String domainURI); +} diff --git a/sca-java-2.x/tags/2.0-Beta4-RC1/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/ManageableResource.java b/sca-java-2.x/tags/2.0-Beta4-RC1/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/ManageableResource.java new file mode 100644 index 0000000000..36051c3908 --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta4-RC1/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/ManageableResource.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.node.manager; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.oasisopen.sca.annotation.Remotable; + +@Remotable +public interface ManageableResource { + + /** + * Ping resource used for service monitoring + * @return + */ + @GET + @Path("/ping") + @Produces(MediaType.TEXT_HTML) + Response ping(); +} diff --git a/sca-java-2.x/tags/2.0-Beta4-RC1/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/ManageableService.java b/sca-java-2.x/tags/2.0-Beta4-RC1/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/ManageableService.java new file mode 100644 index 0000000000..17ab866f44 --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta4-RC1/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/ManageableService.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.node.manager; + +import org.oasisopen.sca.annotation.Remotable; + +@Remotable +public interface ManageableService { + + /** + * isAlive method that allow service to be managed + */ + void isAlive(); +} diff --git a/sca-java-2.x/tags/2.0-Beta4-RC1/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/Status.java b/sca-java-2.x/tags/2.0-Beta4-RC1/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/Status.java new file mode 100644 index 0000000000..f20f5e1b33 --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta4-RC1/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/Status.java @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.node.manager; + +public class Status { + public static int SUCCESS = 0; + public static int FAILURE = 1; + + private String name; + private String uri; + private int status; + private String statusMessage; + private long execution; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getUri() { + return uri; + } + + public void setUri(String uri) { + this.uri = uri; + } + + public int getStatus() { + return status; + } + + public void setStatus(int status) { + this.status = status; + } + + public String getStatusMessage() { + return statusMessage; + } + + public void setStatusMessage(String statusMessage) { + this.statusMessage = statusMessage; + } + + public long getExecution() { + return execution; + } + + public void setExecution(long execution) { + this.execution = execution; + } + + @Override + public String toString() { + return "Status [name=" + name + + ", uri=" + + uri + + ", status=" + + status + + ", statusMessage=" + + statusMessage + + ", execution=" + + execution + + "]"; + } +} diff --git a/sca-java-2.x/tags/2.0-Beta4-RC1/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/impl/DomainAssetManagerResourceImpl.java b/sca-java-2.x/tags/2.0-Beta4-RC1/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/impl/DomainAssetManagerResourceImpl.java new file mode 100644 index 0000000000..3b045b8d1b --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta4-RC1/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/impl/DomainAssetManagerResourceImpl.java @@ -0,0 +1,215 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.node.manager.impl; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; + +import javax.ws.rs.DefaultValue; +import javax.ws.rs.PathParam; +import javax.ws.rs.WebApplicationException; + +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.binding.rest.RESTBinding; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.host.http.client.HttpClientFactory; +import org.apache.tuscany.sca.interfacedef.Interface; +import org.apache.tuscany.sca.node.Node; +import org.apache.tuscany.sca.node.extensibility.NodeActivator; +import org.apache.tuscany.sca.node.extensibility.NodeExtension; +import org.apache.tuscany.sca.node.manager.DomainAssetManagerResource; +import org.apache.tuscany.sca.node.manager.ManageableResource; +import org.apache.tuscany.sca.node.manager.ManageableService; +import org.apache.tuscany.sca.node.manager.Status; +import org.oasisopen.sca.annotation.Init; + +public class DomainAssetManagerResourceImpl implements NodeActivator, DomainAssetManagerResource { + private static Map<String, NodeExtension> nodeMap = new ConcurrentHashMap<String,NodeExtension>(); + + private HttpClientFactory httpClientFactory; + + public void nodeStarted(Node node) { + NodeExtension nodeExtension = (NodeExtension) node; + nodeMap.put(nodeExtension.getDomainURI(), nodeExtension); + } + + public void nodeStopped(Node node) { + NodeExtension nodeExtension = (NodeExtension) node; + nodeMap.remove(nodeExtension.getDomainURI()); + } + + @Init + public void init() { + NodeExtension node = (NodeExtension) nodeMap.values().iterator().next(); + if(node != null) { + ExtensionPointRegistry extensionPoints = node.getExtensionPointRegistry(); + FactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); + this.httpClientFactory = HttpClientFactory.getInstance(extensionPoints); + } + } + + @Override + public List<Status> getResourceStatus(@PathParam("domainURI") @DefaultValue("default") String domainURI) { + if( ! nodeMap.containsKey(domainURI)) { + throw new WebApplicationException(404); + } + + NodeExtension node = nodeMap.get(domainURI); + Composite domainComposite = node.getDomainComposite(); + + List<Status> statuses = new ArrayList<Status>(); + for(Component component : domainComposite.getComponents()) { + for(Service service : component.getServices()) { + Interface interfaceContract = service.getInterfaceContract().getInterface(); + if(ManageableResource.class.getName().equals(interfaceContract.toString())) { + Binding binding = service.getBinding(RESTBinding.class); + if(binding != null) { + + Status status = new Status(); + status.setName(component.getName()); + status.setUri(binding.getURI()); + + try { + Timer t = new Timer(); + status = testResource(status); + status.setExecution(t.elapsed(TimeUnit.MILLISECONDS)); + status.setStatus(Status.SUCCESS); + } catch (Exception e) { + status.setStatus(Status.FAILURE); + status.setStatusMessage(e.getMessage()); + } + + statuses.add(status); + } + } + } + + } + return statuses; + } + + @Override + public List<Status> getServiceStatus(@PathParam("domainURI") @DefaultValue("default") String domainURI) { + if( ! nodeMap.containsKey(domainURI)) { + throw new WebApplicationException(404); + } + + NodeExtension node = nodeMap.get(domainURI); + Composite domainComposite = node.getDomainComposite(); + + List<Status> statuses = new ArrayList<Status>(); + for(Component component : domainComposite.getComponents()) { + for(Service service : component.getServices()) { + Interface interfaceContract = service.getInterfaceContract().getInterface(); + if(ManageableService.class.getName().equals(interfaceContract.toString())) { + + Status status = new Status(); + status.setName(component.getName()); + status.setUri(service.getBindings().get(0).getURI()); + + try { + String serviceName = component.getName() +"/" + service.getName(); + ManageableService serviceInstance = node.getService(ManageableService.class, component.getName()); + Timer t = new Timer(); + serviceInstance.isAlive(); + status.setExecution(t.elapsed(TimeUnit.MILLISECONDS)); + status.setStatus(Status.SUCCESS); + } catch (Exception e) { + status.setStatus(Status.FAILURE); + status.setStatusMessage(e.getMessage()); + } + + statuses.add(status); + } + } + } + + return statuses; + } + + + private Status testResource(Status status) throws IOException { + + // Create an HTTP client + HttpClient httpClient = httpClientFactory.createHttpClient(); + + HttpGet request = new HttpGet(status.getUri() + "ping"); + //request.addHeader("Accept","application/json"); + HttpResponse response = httpClient.execute(request); + + if(response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { + throw new IOException("Error invoking service at '" + request.getURI() +"' => " + response.getStatusLine().getReasonPhrase()); + } + + if (httpClient != null) { + httpClient.getConnectionManager().shutdown(); + } + + return status; + } + + + class Timer { + Date time; + long t; + + public Timer() { + reset(); + } + + public void reset() { + time = new Date(); + t = System.nanoTime(); + } + + public Date time() { + return time; + } + + public long elapsed(TimeUnit timeUnit) { + long elapsedTime = elapsed(); + return timeUnit.convert(elapsedTime, TimeUnit.NANOSECONDS); + } + + public void print(String s, TimeUnit timeUnit) { + long elapsedTime = elapsed(); + + System.out.println(s + ": " + timeUnit.convert(elapsedTime, TimeUnit.NANOSECONDS) + "ms"); + } + + private long elapsed() { + return System.nanoTime() - t; + } + } + +} diff --git a/sca-java-2.x/tags/2.0-Beta4-RC1/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/impl/DomainCompositeResourceImpl.java b/sca-java-2.x/tags/2.0-Beta4-RC1/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/impl/DomainCompositeResourceImpl.java new file mode 100644 index 0000000000..9cb91e2917 --- /dev/null +++ b/sca-java-2.x/tags/2.0-Beta4-RC1/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/impl/DomainCompositeResourceImpl.java @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.node.manager.impl; + +import java.io.ByteArrayOutputStream; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import javax.ws.rs.WebApplicationException; +import javax.xml.namespace.QName; +import javax.xml.stream.XMLOutputFactory; + +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.node.Node; +import org.apache.tuscany.sca.node.extensibility.NodeActivator; +import org.apache.tuscany.sca.node.extensibility.NodeExtension; +import org.apache.tuscany.sca.node.manager.DomainCompositeResource; + +public class DomainCompositeResourceImpl implements NodeActivator, DomainCompositeResource { + private static Map<String, NodeExtension> nodeMap = new ConcurrentHashMap<String,NodeExtension>(); + + public void nodeStarted(Node node) { + NodeExtension nodeExtension = (NodeExtension) node; + nodeMap.put(nodeExtension.getDomainURI(), nodeExtension); + } + + public void nodeStopped(Node node) { + NodeExtension nodeExtension = (NodeExtension) node; + nodeMap.remove(nodeExtension.getDomainURI()); + } + + public String getDomainComposite(String domainURI) { + if( ! nodeMap.containsKey(domainURI)) { + throw new WebApplicationException(404); + } + + NodeExtension node = nodeMap.get(domainURI); + Composite composite = node.getDomainComposite(); + + //set name, as it's empty by default + composite.setName(new QName("", "Domain")); + + ExtensionPointRegistry registry = node.getExtensionPointRegistry(); + StAXArtifactProcessorExtensionPoint xmlProcessors = + registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + StAXArtifactProcessor<Composite> compositeProcessor = + xmlProcessors.getProcessor(Composite.class); + + return writeComposite(composite, registry, compositeProcessor); + } + + + private String writeComposite(Composite composite, ExtensionPointRegistry registry, StAXArtifactProcessor<Composite> compositeProcessor){ + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + XMLOutputFactory outputFactory = + registry.getExtensionPoint(FactoryExtensionPoint.class) + .getFactory(XMLOutputFactory.class); + + try { + compositeProcessor.write(composite, outputFactory.createXMLStreamWriter(bos), new ProcessorContext(registry)); + } catch(Exception ex) { + return ex.toString(); + } + + String result = bos.toString(); + + // write out and nested composites + for (Component component : composite.getComponents()) { + if (component.getImplementation() instanceof Composite) { + result += "\n<!-- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -->\n" + + writeComposite((Composite)component.getImplementation(), registry, + compositeProcessor); + } + } + return result; + } +} |