From e5b7380c874745c989d1816b8f552504f038e1bc Mon Sep 17 00:00:00 2001 From: lresende Date: Thu, 26 Sep 2013 20:33:20 +0000 Subject: 2.0 branch for possible maintenance release git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1526672 13f79535-47bb-0310-9956-ffa450edef68 --- .../node/manager/DomainAssetManagerResource.java | 39 +++++ .../sca/node/manager/DomainCompositeResource.java | 36 +++++ .../tuscany/sca/node/manager/Manageable.java | 31 ++++ .../apache/tuscany/sca/node/manager/Status.java | 87 +++++++++++ .../impl/DomainAssetManagerResourceImpl.java | 160 +++++++++++++++++++++ .../manager/impl/DomainCompositeResourceImpl.java | 100 +++++++++++++ 6 files changed, 453 insertions(+) create mode 100644 sca-java-2.x/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/DomainAssetManagerResource.java create mode 100644 sca-java-2.x/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/DomainCompositeResource.java create mode 100644 sca-java-2.x/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/Manageable.java create mode 100644 sca-java-2.x/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/Status.java create mode 100644 sca-java-2.x/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/impl/DomainAssetManagerResourceImpl.java create mode 100644 sca-java-2.x/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/impl/DomainCompositeResourceImpl.java (limited to 'sca-java-2.x/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager') diff --git a/sca-java-2.x/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/DomainAssetManagerResource.java b/sca-java-2.x/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/DomainAssetManagerResource.java new file mode 100644 index 0000000000..8a1fab65b7 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/DomainAssetManagerResource.java @@ -0,0 +1,39 @@ +/* + * 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 +@Path("") +public interface DomainAssetManagerResource { + + @GET + @Path("{domainURI}/services/status") + List getServiceStatus(@PathParam("domainURI") @DefaultValue("default") String domainURI); + +} diff --git a/sca-java-2.x/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/DomainCompositeResource.java b/sca-java-2.x/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/DomainCompositeResource.java new file mode 100644 index 0000000000..f917ca3b99 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/DomainCompositeResource.java @@ -0,0 +1,36 @@ +/* + * 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 +@Path("") +public interface DomainCompositeResource { + + @GET + @Path("{domainURI}") + String getDomainComposite(@PathParam("domainURI") @DefaultValue("default") String domainURI); +} diff --git a/sca-java-2.x/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/Manageable.java b/sca-java-2.x/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/Manageable.java new file mode 100644 index 0000000000..139e87c5f5 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/Manageable.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 Manageable { + + /** + * isAlive method that allow service to be managed + */ + void isAlive(); +} diff --git a/sca-java-2.x/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/Status.java b/sca-java-2.x/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/Status.java new file mode 100644 index 0000000000..75d25734d8 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/Status.java @@ -0,0 +1,87 @@ +/* + * 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 String OK ="ok"; + public static String WARNING = "warning"; + public static String CRITICAL = "critical"; + public static String UNAVAILABLE = "unavailable"; + + private String name; + private String uri; + private String 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 String getStatus() { + return status; + } + + public void setStatus(String 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/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/impl/DomainAssetManagerResourceImpl.java b/sca-java-2.x/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/impl/DomainAssetManagerResourceImpl.java new file mode 100644 index 0000000000..3935c75021 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/impl/DomainAssetManagerResourceImpl.java @@ -0,0 +1,160 @@ +/* + * 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.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.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.Service; +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.Manageable; +import org.apache.tuscany.sca.node.manager.Status; +import org.oasisopen.sca.annotation.Init; +import org.oasisopen.sca.annotation.Property; +import org.oasisopen.sca.annotation.Scope; + +@Scope("COMPOSITE") +public class DomainAssetManagerResourceImpl implements NodeActivator, DomainAssetManagerResource { + private static Map nodeMap = new ConcurrentHashMap(); + + @Property + private int warningThreshold; + + @Property + private int criticalThreshold; + + @Property + private int unavailableThreshold; + + 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() { + System.out.println("Initializing Domain Asset Manager"); + System.out.println(" - Warning threshold : " + warningThreshold + " ms" ); + System.out.println(" - Critical threshold : " + criticalThreshold + " ms"); + System.out.println(" - Unavailable threshold : " + unavailableThreshold + " ms"); + + } + + @Override + public List 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 statuses = new ArrayList(); + for(Component component : domainComposite.getComponents()) { + for(Service service : component.getServices()) { + Interface interfaceContract = service.getInterfaceContract().getInterface(); + if(Manageable.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(); + Manageable serviceInstance = node.getService(Manageable.class, serviceName); + Timer t = new Timer(); + serviceInstance.isAlive(); + status.setExecution(t.elapsed(TimeUnit.MILLISECONDS)); + + if(status.getExecution() < warningThreshold) { + status.setStatus(Status.OK); + }else if(status.getExecution() > warningThreshold) { + status.setStatus(Status.WARNING); + }else if(status.getExecution() > criticalThreshold) { + status.setStatus(Status.CRITICAL); + } else { + status.setStatus(Status.UNAVAILABLE); + } + } catch (Exception e) { + status.setStatus(Status.UNAVAILABLE); + status.setStatusMessage(e.getMessage()); + } + + statuses.add(status); + } + } + } + + return statuses; + } + + 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/branches/2.0/modules/node-manager/src/main/java/org/apache/tuscany/sca/node/manager/impl/DomainCompositeResourceImpl.java b/sca-java-2.x/branches/2.0/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/branches/2.0/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 nodeMap = new ConcurrentHashMap(); + + 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 compositeProcessor = + xmlProcessors.getProcessor(Composite.class); + + return writeComposite(composite, registry, compositeProcessor); + } + + + private String writeComposite(Composite composite, ExtensionPointRegistry registry, StAXArtifactProcessor 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\n" + + writeComposite((Composite)component.getImplementation(), registry, + compositeProcessor); + } + } + return result; + } +} -- cgit v1.2.3