You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2013/11/22 15:42:34 UTC
svn commit: r1544554 - in /karaf/trunk/instance/core/src:
main/java/org/apache/karaf/instance/core/
main/java/org/apache/karaf/instance/core/internal/
main/resources/OSGI-INF/blueprint/
test/java/org/apache/karaf/instance/core/management/internal/
Author: jbonofre
Date: Fri Nov 22 14:42:33 2013
New Revision: 1544554
URL: http://svn.apache.org/r1544554
Log:
[KARAF-2264] Rename Instances to InstansMBeanImpl and wrap exceptions as MBeanException
Added:
karaf/trunk/instance/core/src/main/java/org/apache/karaf/instance/core/internal/InstancesMBeanImpl.java
Removed:
karaf/trunk/instance/core/src/main/java/org/apache/karaf/instance/core/internal/Instances.java
Modified:
karaf/trunk/instance/core/src/main/java/org/apache/karaf/instance/core/InstancesMBean.java
karaf/trunk/instance/core/src/main/resources/OSGI-INF/blueprint/instance-core.xml
karaf/trunk/instance/core/src/test/java/org/apache/karaf/instance/core/management/internal/InstanceServiceMBeanImplTest.java
Modified: karaf/trunk/instance/core/src/main/java/org/apache/karaf/instance/core/InstancesMBean.java
URL: http://svn.apache.org/viewvc/karaf/trunk/instance/core/src/main/java/org/apache/karaf/instance/core/InstancesMBean.java?rev=1544554&r1=1544553&r2=1544554&view=diff
==============================================================================
--- karaf/trunk/instance/core/src/main/java/org/apache/karaf/instance/core/InstancesMBean.java (original)
+++ karaf/trunk/instance/core/src/main/java/org/apache/karaf/instance/core/InstancesMBean.java Fri Nov 22 14:42:33 2013
@@ -16,6 +16,7 @@
*/
package org.apache.karaf.instance.core;
+import javax.management.MBeanException;
import javax.management.openmbean.TabularData;
public interface InstancesMBean {
@@ -34,21 +35,21 @@ public interface InstancesMBean {
INSTANCE_RMI_SERVER_PORT, INSTANCE_STATE, INSTANCE_LOCATION, INSTANCE_JAVAOPTS };
// Operations
- int createInstance(String name, int sshPort, int rmiRegistryPort, int rmiServerPort, String location, String javaOpts, String features, String featureURLs) throws Exception;
- void changeSshPort(String name, int port) throws Exception;
- void changeRmiRegistryPort(String name, int port) throws Exception;
- void changeRmiServerPort(String name, int port) throws Exception;
- void changeJavaOpts(String name, String javaopts) throws Exception;
- void destroyInstance(String name) throws Exception;
- void startInstance(String name) throws Exception;
- void startInstance(String name, String opts) throws Exception;
- void startInstance(String name, String opts, boolean wait, boolean debug) throws Exception;
- void stopInstance(String name) throws Exception;
- void renameInstance(String originalName, String newName) throws Exception;
- void renameInstance(String originalName, String newName, boolean verbose) throws Exception;
- void cloneInstance(String name, String cloneName, int sshPort, int rmiRegistryPort, int rmiServerPort, String location, String javaOpts) throws Exception;
+ int createInstance(String name, int sshPort, int rmiRegistryPort, int rmiServerPort, String location, String javaOpts, String features, String featureURLs) throws MBeanException;
+ void changeSshPort(String name, int port) throws MBeanException;
+ void changeRmiRegistryPort(String name, int port) throws MBeanException;
+ void changeRmiServerPort(String name, int port) throws MBeanException;
+ void changeJavaOpts(String name, String javaopts) throws MBeanException;
+ void destroyInstance(String name) throws MBeanException;
+ void startInstance(String name) throws MBeanException;
+ void startInstance(String name, String opts) throws MBeanException;
+ void startInstance(String name, String opts, boolean wait, boolean debug) throws MBeanException;
+ void stopInstance(String name) throws MBeanException;
+ void renameInstance(String originalName, String newName) throws MBeanException;
+ void renameInstance(String originalName, String newName, boolean verbose) throws MBeanException;
+ void cloneInstance(String name, String cloneName, int sshPort, int rmiRegistryPort, int rmiServerPort, String location, String javaOpts) throws MBeanException;
// Attributes
- TabularData getInstances() throws Exception;
+ TabularData getInstances() throws MBeanException;
}
Added: karaf/trunk/instance/core/src/main/java/org/apache/karaf/instance/core/internal/InstancesMBeanImpl.java
URL: http://svn.apache.org/viewvc/karaf/trunk/instance/core/src/main/java/org/apache/karaf/instance/core/internal/InstancesMBeanImpl.java?rev=1544554&view=auto
==============================================================================
--- karaf/trunk/instance/core/src/main/java/org/apache/karaf/instance/core/internal/InstancesMBeanImpl.java (added)
+++ karaf/trunk/instance/core/src/main/java/org/apache/karaf/instance/core/internal/InstancesMBeanImpl.java Fri Nov 22 14:42:33 2013
@@ -0,0 +1,224 @@
+/*
+ * 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.karaf.instance.core.internal;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import javax.management.MBeanException;
+import javax.management.NotCompliantMBeanException;
+import javax.management.StandardMBean;
+import javax.management.openmbean.TabularData;
+
+import org.apache.karaf.instance.core.Instance;
+import org.apache.karaf.instance.core.InstanceSettings;
+import org.apache.karaf.instance.core.InstancesMBean;
+
+public class InstancesMBeanImpl extends StandardMBean implements InstancesMBean {
+
+ static final String DEBUG_OPTS = " -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005";
+ static final String DEFAULT_OPTS = "-server -Xmx512M -Dcom.sun.management.jmxremote";
+
+ private org.apache.karaf.instance.core.InstanceService instanceService;
+
+ public InstancesMBeanImpl(org.apache.karaf.instance.core.InstanceService instanceService) throws NotCompliantMBeanException {
+ super(InstancesMBean.class);
+ this.instanceService = instanceService;
+ }
+
+ public int createInstance(String name, int sshPort, int rmiRegistryPort, int rmiServerPort, String location, String javaOpts, String features, String featureURLs)
+ throws MBeanException {
+ try {
+ if ("".equals(location)) {
+ location = null;
+ }
+ if ("".equals(javaOpts)) {
+ javaOpts = null;
+ }
+
+ InstanceSettings settings = new InstanceSettings(sshPort, rmiRegistryPort, rmiServerPort, location, javaOpts,
+ parseStringList(featureURLs), parseStringList(features));
+
+ Instance inst = instanceService.createInstance(name, settings, false);
+ if (inst != null) {
+ return inst.getPid();
+ } else {
+ return -1;
+ }
+ } catch (Exception e) {
+ throw new MBeanException(null, e.getMessage());
+ }
+ }
+
+ public void changeSshPort(String name, int port) throws MBeanException {
+ try {
+ getExistingInstance(name).changeSshPort(port);
+ } catch (Exception e) {
+ throw new MBeanException(null, e.getMessage());
+ }
+ }
+
+ public void changeRmiRegistryPort(String name, int port) throws MBeanException {
+ try {
+ getExistingInstance(name).changeRmiRegistryPort(port);
+ } catch (Exception e) {
+ throw new MBeanException(null, e.getMessage());
+ }
+ }
+
+ public void changeRmiServerPort(String name, int port) throws MBeanException {
+ try {
+ getExistingInstance(name).changeRmiServerPort(port);
+ } catch (Exception e) {
+ throw new MBeanException(null, e.getMessage());
+ }
+ }
+
+ public void changeJavaOpts(String name, String javaOpts) throws MBeanException {
+ try {
+ getExistingInstance(name).changeJavaOpts(javaOpts);
+ } catch (Exception e) {
+ throw new MBeanException(null, e.getMessage());
+ }
+ }
+
+ public void destroyInstance(String name) throws MBeanException {
+ try {
+ getExistingInstance(name).destroy();
+ } catch (Exception e) {
+ throw new MBeanException(null, e.getMessage());
+ }
+ }
+
+ public void startInstance(String name) throws MBeanException {
+ try {
+ getExistingInstance(name).start(null);
+ } catch (Exception e) {
+ throw new MBeanException(null, e.getMessage());
+ }
+ }
+
+ public void startInstance(String name, String opts) throws MBeanException {
+ try {
+ getExistingInstance(name).start(opts);
+ } catch (Exception e) {
+ throw new MBeanException(null, e.getMessage());
+ }
+ }
+
+ public void startInstance(String name, String opts, boolean wait, boolean debug) throws MBeanException {
+ try {
+ Instance child = getExistingInstance(name);
+ String options = opts;
+ if (options == null) {
+ options = child.getJavaOpts();
+ }
+ if (options == null) {
+ options = DEFAULT_OPTS;
+ }
+ if (debug) {
+ options += DEBUG_OPTS;
+ }
+ if (wait) {
+ String state = child.getState();
+ if (Instance.STOPPED.equals(state)) {
+ child.start(opts);
+ }
+ if (!Instance.STARTED.equals(state)) {
+ do {
+ Thread.sleep(500);
+ state = child.getState();
+ } while (Instance.STARTING.equals(state));
+ }
+ } else {
+ child.start(opts);
+ }
+ } catch (Exception e) {
+ throw new MBeanException(null, e.getMessage());
+ }
+ }
+
+ public void stopInstance(String name) throws MBeanException {
+ try {
+ getExistingInstance(name).stop();
+ } catch (Exception e) {
+ throw new MBeanException(null, e.getMessage());
+ }
+ }
+
+ public void renameInstance(String originalName, String newName) throws MBeanException {
+ try {
+ instanceService.renameInstance(originalName, newName, false);
+ } catch (Exception e) {
+ throw new MBeanException(null, e.getMessage());
+ }
+ }
+
+ public void renameInstance(String originalName, String newName, boolean verbose) throws MBeanException {
+ try {
+ instanceService.renameInstance(originalName, newName, verbose);
+ } catch (Exception e) {
+ throw new MBeanException(null, e.getMessage());
+ }
+ }
+
+ public void cloneInstance(String name, String cloneName, int sshPort, int rmiRegistryPort, int rmiServerPort, String location, String javaOpts) throws MBeanException {
+ try {
+ if ("".equals(location)) {
+ location = null;
+ }
+ if ("".equals(javaOpts)) {
+ javaOpts = null;
+ }
+
+ InstanceSettings settings = new InstanceSettings(sshPort, rmiRegistryPort, rmiServerPort, location, javaOpts, null, null);
+
+ instanceService.cloneInstance(name, cloneName, settings, false);
+ } catch (Exception e) {
+ throw new MBeanException(null, e.getMessage());
+ }
+ }
+
+ public TabularData getInstances() throws MBeanException {
+ List<Instance> instances = Arrays.asList(instanceService.getInstances());
+ TabularData table = InstanceToTableMapper.tableFrom(instances);
+ return table;
+ }
+
+ private Instance getExistingInstance(String name) {
+ Instance i = instanceService.getInstance(name);
+ if (i == null) {
+ throw new IllegalArgumentException("Instance '" + name + "' does not exist");
+ }
+ return i;
+ }
+
+ private List<String> parseStringList(String value) {
+ List<String> list = new ArrayList<String>();
+ if (value != null) {
+ for (String el : value.split(",")) {
+ String trimmed = el.trim();
+ if (trimmed.length() == 0) {
+ continue;
+ }
+ list.add(trimmed);
+ }
+ }
+ return list;
+ }
+}
Modified: karaf/trunk/instance/core/src/main/resources/OSGI-INF/blueprint/instance-core.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/instance/core/src/main/resources/OSGI-INF/blueprint/instance-core.xml?rev=1544554&r1=1544553&r2=1544554&view=diff
==============================================================================
--- karaf/trunk/instance/core/src/main/resources/OSGI-INF/blueprint/instance-core.xml (original)
+++ karaf/trunk/instance/core/src/main/resources/OSGI-INF/blueprint/instance-core.xml Fri Nov 22 14:42:33 2013
@@ -28,11 +28,11 @@
<service ref="instanceService" interface="org.apache.karaf.instance.core.InstanceService"/>
- <bean id="mbeanImpl" class="org.apache.karaf.instance.core.internal.Instances">
+ <bean id="instancesMBean" class="org.apache.karaf.instance.core.internal.InstancesMBeanImpl">
<argument ref="instanceService" />
</bean>
- <service ref="mbeanImpl" auto-export="interfaces">
+ <service ref="instancesMBean" auto-export="interfaces">
<service-properties>
<entry key="jmx.objectname" value="org.apache.karaf:type=instance,name=${karaf.name}"/>
</service-properties>
Modified: karaf/trunk/instance/core/src/test/java/org/apache/karaf/instance/core/management/internal/InstanceServiceMBeanImplTest.java
URL: http://svn.apache.org/viewvc/karaf/trunk/instance/core/src/test/java/org/apache/karaf/instance/core/management/internal/InstanceServiceMBeanImplTest.java?rev=1544554&r1=1544553&r2=1544554&view=diff
==============================================================================
--- karaf/trunk/instance/core/src/test/java/org/apache/karaf/instance/core/management/internal/InstanceServiceMBeanImplTest.java (original)
+++ karaf/trunk/instance/core/src/test/java/org/apache/karaf/instance/core/management/internal/InstanceServiceMBeanImplTest.java Fri Nov 22 14:42:33 2013
@@ -28,7 +28,7 @@ import org.apache.karaf.instance.core.In
import org.apache.karaf.instance.core.InstanceService;
import org.apache.karaf.instance.core.InstanceSettings;
import org.apache.karaf.instance.core.InstancesMBean;
-import org.apache.karaf.instance.core.internal.Instances;
+import org.apache.karaf.instance.core.internal.InstancesMBeanImpl;
import org.easymock.EasyMock;
import org.junit.Assert;
@@ -46,7 +46,7 @@ public class InstanceServiceMBeanImplTes
EasyMock.expect(instanceService.createInstance("t1", instanceSettings, false)).andReturn(inst);
EasyMock.replay(instanceService);
- Instances ab = new Instances(instanceService);
+ InstancesMBeanImpl ab = new InstancesMBeanImpl(instanceService);
assertEquals(42, ab.createInstance("t1", 123, 456, 789, "somewhere", "someopts", " webconsole, funfeat", ""));
}
@@ -58,7 +58,7 @@ public class InstanceServiceMBeanImplTes
EasyMock.expect(instanceService.createInstance("t1", instanceSettings, false)).andReturn(null);
EasyMock.replay(instanceService);
- InstancesMBean ab = new Instances(instanceService);
+ InstancesMBean ab = new InstancesMBeanImpl(instanceService);
assertEquals(-1, ab.createInstance("t1", 0, 0, 0, "", "", "", ""));
}
@@ -82,7 +82,7 @@ public class InstanceServiceMBeanImplTes
EasyMock.expect(instanceService.getInstances()).andReturn(new Instance[]{i1, i2});
EasyMock.replay(instanceService);
- Instances instanceServiceMBean = new Instances(instanceService);
+ InstancesMBeanImpl instanceServiceMBean = new InstancesMBeanImpl(instanceService);
TabularData tabularData = instanceServiceMBean.getInstances();
Assert.assertEquals(2, tabularData.size());
CompositeData cd1 = tabularData.get(new Object[]{"i1"});
@@ -110,7 +110,7 @@ public class InstanceServiceMBeanImplTes
EasyMock.expect(instanceService.getInstance("test instance")).andReturn(inst);
EasyMock.replay(instanceService);
- InstancesMBean instanceServiceMBean = new Instances(instanceService);
+ InstancesMBean instanceServiceMBean = new InstancesMBeanImpl(instanceService);
instanceServiceMBean.startInstance("test instance", "-x -y -z");
EasyMock.verify(instanceService);
@@ -127,7 +127,7 @@ public class InstanceServiceMBeanImplTes
EasyMock.expect(instanceService.getInstance("test instance")).andReturn(inst);
EasyMock.replay(instanceService);
- InstancesMBean instanceServiceMBean = new Instances(instanceService);
+ InstancesMBean instanceServiceMBean = new InstancesMBeanImpl(instanceService);
instanceServiceMBean.startInstance("test instance", null);
EasyMock.verify(instanceService);
@@ -144,7 +144,7 @@ public class InstanceServiceMBeanImplTes
EasyMock.expect(instanceService.getInstance("test instance")).andReturn(inst);
EasyMock.replay(instanceService);
- InstancesMBean instanceServiceMBean = new Instances(instanceService);
+ InstancesMBean instanceServiceMBean = new InstancesMBeanImpl(instanceService);
instanceServiceMBean.stopInstance("test instance");
EasyMock.verify(instanceService);
@@ -161,7 +161,7 @@ public class InstanceServiceMBeanImplTes
EasyMock.expect(instanceService.getInstance("test instance")).andReturn(inst);
EasyMock.replay(instanceService);
- InstancesMBean instanceServiceMBean = new Instances(instanceService);
+ InstancesMBean instanceServiceMBean = new InstancesMBeanImpl(instanceService);
instanceServiceMBean.destroyInstance("test instance");
EasyMock.verify(instanceService);
@@ -178,7 +178,7 @@ public class InstanceServiceMBeanImplTes
EasyMock.expect(instanceService.getInstance("test instance")).andReturn(inst);
EasyMock.replay(instanceService);
- InstancesMBean instanceServiceMBean = new Instances(instanceService);
+ InstancesMBean instanceServiceMBean = new InstancesMBeanImpl(instanceService);
instanceServiceMBean.changeSshPort("test instance", 7788);
EasyMock.verify(instanceService);
@@ -195,7 +195,7 @@ public class InstanceServiceMBeanImplTes
EasyMock.expect(instanceService.getInstance("test instance")).andReturn(inst);
EasyMock.replay(instanceService);
- InstancesMBean instanceServiceMBean = new Instances(instanceService);
+ InstancesMBean instanceServiceMBean = new InstancesMBeanImpl(instanceService);
instanceServiceMBean.changeRmiRegistryPort("test instance", 1123);
EasyMock.verify(instanceService);
@@ -212,7 +212,7 @@ public class InstanceServiceMBeanImplTes
EasyMock.expect(instanceService.getInstance("test instance")).andReturn(inst);
EasyMock.replay(instanceService);
- InstancesMBean instanceServiceMBean = new Instances(instanceService);
+ InstancesMBean instanceServiceMBean = new InstancesMBeanImpl(instanceService);
instanceServiceMBean.changeRmiServerPort("test instance", 44444);
EasyMock.verify(instanceService);
@@ -229,7 +229,7 @@ public class InstanceServiceMBeanImplTes
EasyMock.expect(instanceService.getInstance("test instance")).andReturn(inst);
EasyMock.replay(instanceService);
- InstancesMBean instanceServiceMBean = new Instances(instanceService);
+ InstancesMBean instanceServiceMBean = new InstancesMBeanImpl(instanceService);
instanceServiceMBean.changeJavaOpts("test instance", "new opts");
EasyMock.verify(instanceService);