From 61d1f49c702f45d9900029af3bd65f31ba384455 Mon Sep 17 00:00:00 2001 From: Christian Schneider Date: Tue, 24 Nov 2015 12:30:20 +0100 Subject: [PATCH] Switch to DS --- .../artemis/jlibaio/LibaioContext.java | 10 +- artemis-server-osgi/pom.xml | 1 - .../activemq/artemis/osgi/Activator.java | 30 --- .../artemis/osgi/ArtemisBrokerFactory.java | 85 -------- .../osgi/BaseManagedServiceFactory.java | 195 ------------------ .../activemq/artemis/osgi/OsgiBroker.java | 68 +++--- .../artemis/osgi/ProtocolTrackerTest.java | 1 + 7 files changed, 37 insertions(+), 353 deletions(-) delete mode 100644 artemis-server-osgi/src/main/java/org/apache/activemq/artemis/osgi/Activator.java delete mode 100644 artemis-server-osgi/src/main/java/org/apache/activemq/artemis/osgi/ArtemisBrokerFactory.java delete mode 100644 artemis-server-osgi/src/main/java/org/apache/activemq/artemis/osgi/BaseManagedServiceFactory.java diff --git a/artemis-native/src/main/java/org/apache/activemq/artemis/jlibaio/LibaioContext.java b/artemis-native/src/main/java/org/apache/activemq/artemis/jlibaio/LibaioContext.java index 35b04c8a8b..be5188fc6b 100644 --- a/artemis-native/src/main/java/org/apache/activemq/artemis/jlibaio/LibaioContext.java +++ b/artemis-native/src/main/java/org/apache/activemq/artemis/jlibaio/LibaioContext.java @@ -307,16 +307,16 @@ public class LibaioContext implements Closeable { return new LibaioFile<>(res, null); } - + /** * Checks that the given argument is not null. If it is, throws {@link NullPointerException}. * Otherwise, returns the argument. */ private static T checkNotNull(T arg, String text) { - if (arg == null) { - throw new NullPointerException(text); - } - return arg; + if (arg == null) { + throw new NullPointerException(text); + } + return arg; } /** diff --git a/artemis-server-osgi/pom.xml b/artemis-server-osgi/pom.xml index d4928636b9..82ca69b688 100644 --- a/artemis-server-osgi/pom.xml +++ b/artemis-server-osgi/pom.xml @@ -112,7 +112,6 @@ *;scope=compile|runtime - org.apache.activemq.artemis.osgi.Activator <_exportcontents>* diff --git a/artemis-server-osgi/src/main/java/org/apache/activemq/artemis/osgi/Activator.java b/artemis-server-osgi/src/main/java/org/apache/activemq/artemis/osgi/Activator.java deleted file mode 100644 index d135b62556..0000000000 --- a/artemis-server-osgi/src/main/java/org/apache/activemq/artemis/osgi/Activator.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.apache.activemq.artemis.osgi; - -import java.util.Hashtable; - -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; -import org.osgi.framework.ServiceRegistration; -import org.osgi.service.cm.ManagedServiceFactory; - -public class Activator implements BundleActivator { - - ArtemisBrokerFactory factory; - ServiceRegistration registration; - - @Override - public void start(BundleContext context) throws Exception { - factory = new ArtemisBrokerFactory(context); - Hashtable props = new Hashtable<>(); - props.put(Constants.SERVICE_PID, ArtemisBrokerFactory.PID); - registration = context.registerService(ManagedServiceFactory.class, factory, props); - } - - @Override - public void stop(BundleContext context) throws Exception { - registration.unregister(); - factory.destroy(); - } - -} \ No newline at end of file diff --git a/artemis-server-osgi/src/main/java/org/apache/activemq/artemis/osgi/ArtemisBrokerFactory.java b/artemis-server-osgi/src/main/java/org/apache/activemq/artemis/osgi/ArtemisBrokerFactory.java deleted file mode 100644 index 132e9b3f1a..0000000000 --- a/artemis-server-osgi/src/main/java/org/apache/activemq/artemis/osgi/ArtemisBrokerFactory.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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.activemq.artemis.osgi; - -import java.util.Dictionary; - -import org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager; -import org.jboss.logging.Logger; -import org.osgi.framework.BundleContext; - -public class ArtemisBrokerFactory extends BaseManagedServiceFactory { - - public static final String PID = "org.apache.activemq.artemis"; - - private static final Logger LOGGER = Logger.getLogger(ArtemisBrokerFactory.class); - - public ArtemisBrokerFactory(BundleContext context) { - super(context, ArtemisBrokerFactory.class.getName()); - } - - @Override - protected OsgiBroker doCreate(Dictionary properties) throws Exception { - String config = getMandatory(properties, "config"); - String name = getMandatory(properties, "name"); - String domain = getMandatory(properties, "domain"); - - ActiveMQJAASSecurityManager security = new ActiveMQJAASSecurityManager(domain); - String serverInstanceDir = null; - String karafDataDir = System.getProperty("karaf.data"); - if (karafDataDir != null) { - serverInstanceDir = karafDataDir + "/artemis/" + name; - } - OsgiBroker server = new OsgiBroker(getContext(), name, serverInstanceDir, config, security); - server.start(); - return server; - } - - private String getMandatory(Dictionary properties, String key) { - String value = (String) properties.get(key); - if (value == null) { - throw new IllegalStateException("Property " + key + " must be set"); - } - return value; - } - - @Override - protected void doDestroy(OsgiBroker broker) throws Exception { - broker.stop(); - } - - @Override - protected OsgiBroker register(OsgiBroker broker, Dictionary properties) { - broker.register(getContext(), properties); - return broker; - } - - @Override - protected void unregister(OsgiBroker broker) { - broker.unregister(); - } - - @Override - protected void warn(String message, Throwable t) { - LOGGER.warn(message, t); - } - - @Override - protected void info(String message, Throwable t) { - LOGGER.info(message, t); - } -} \ No newline at end of file diff --git a/artemis-server-osgi/src/main/java/org/apache/activemq/artemis/osgi/BaseManagedServiceFactory.java b/artemis-server-osgi/src/main/java/org/apache/activemq/artemis/osgi/BaseManagedServiceFactory.java deleted file mode 100644 index a8ee27a520..0000000000 --- a/artemis-server-osgi/src/main/java/org/apache/activemq/artemis/osgi/BaseManagedServiceFactory.java +++ /dev/null @@ -1,195 +0,0 @@ -/* - * 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.activemq.artemis.osgi; - -import java.util.Dictionary; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; - -import org.osgi.framework.BundleContext; -import org.osgi.service.cm.ConfigurationException; -import org.osgi.service.cm.ManagedServiceFactory; - -public abstract class BaseManagedServiceFactory implements ManagedServiceFactory { - - public static final long DEFAULT_TIMEOUT_BEFORE_INTERRUPT = 30000; - - private final BundleContext context; - private final String name; - private final long timeoutBeforeInterrupt; - private final AtomicBoolean destroyed; - private final ExecutorService executor; - private final Map> services; - - public BaseManagedServiceFactory(BundleContext context, String name) { - this(context, name, DEFAULT_TIMEOUT_BEFORE_INTERRUPT); - } - - public BaseManagedServiceFactory(BundleContext context, String name, long timeoutBeforeInterrupt) { - this.context = context; - this.name = name; - this.timeoutBeforeInterrupt = timeoutBeforeInterrupt; - this.destroyed = new AtomicBoolean(false); - this.executor = Executors.newSingleThreadExecutor(); - this.services = new ConcurrentHashMap<>(); - } - - public String getName() { - return name; - } - - public BundleContext getContext() { - return context; - } - - public void updated(final String pid, final Dictionary properties) throws ConfigurationException { - if (destroyed.get()) { - return; - } - executor.submit(new Runnable() { - public void run() { - try { - internalUpdate(pid, properties); - } catch (Throwable t) { - warn("Error destroying service for ManagedServiceFactory " + getName(), t); - } - } - }); - } - - public void deleted(final String pid) { - if (destroyed.get()) { - return; - } - executor.submit(new Runnable() { - public void run() { - try { - internalDelete(pid); - } catch (Throwable throwable) { - warn("Error destroying service for ManagedServiceFactory " + getName(), throwable); - } - } - }); - } - - private void internalUpdate(String pid, Dictionary properties) { - Pair pair = services.remove(pid); - if (pair != null) { - internalDelete(pid); - } - if (destroyed.get()) { - return; - } - try { - T t = doCreate(properties); - try { - if (destroyed.get()) { - throw new IllegalStateException("ManagedServiceFactory has been destroyed"); - } - R registration = register(t, properties); - services.put(pid, new Pair<>(t, registration)); - } catch (Throwable throwable1) { - try { - doDestroy(t); - } catch (Throwable throwable2) { - // Ignore - } - throw throwable1; - } - } catch (Throwable throwable) { - warn("Error creating service for ManagedServiceFactory " + getName(), throwable); - } - } - - private void internalDelete(String pid) { - Pair pair = services.remove(pid); - if (pair != null) { - try { - unregister(pair.getSecond()); - } catch (Throwable t) { - info("Error unregistering service", t); - } - try { - doDestroy(pair.getFirst()); - } catch (Throwable t) { - info("Error destroying service", t); - } - } - } - - protected abstract T doCreate(Dictionary properties) throws Exception; - - protected abstract void doDestroy(T t) throws Exception; - - protected abstract R register(T t, Dictionary properties); - - protected abstract void unregister(R registration); - - protected abstract void warn(String message, Throwable t); - - protected abstract void info(String message, Throwable t); - - public void destroy() { - if (destroyed.compareAndSet(false, true)) { - executor.shutdown(); - try { - executor.awaitTermination(timeoutBeforeInterrupt, TimeUnit.MILLISECONDS); - } catch (InterruptedException e) { - throw new RuntimeException("Shutdown interrupted"); - } - if (!executor.isTerminated()) { - executor.shutdownNow(); - try { - executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); - } catch (InterruptedException e) { - throw new RuntimeException("Shutdown interrupted"); - } - } - - while (!services.isEmpty()) { - String pid = services.keySet().iterator().next(); - internalDelete(pid); - } - } - } - - static class Pair { - private U first; - private V second; - public Pair(U first, V second) { - this.first = first; - this.second = second; - } - public U getFirst() { - return first; - } - public V getSecond() { - return second; - } - public void setFirst(U first) { - this.first = first; - } - public void setSecond(V second) { - this.second = second; - } - } - -} diff --git a/artemis-server-osgi/src/main/java/org/apache/activemq/artemis/osgi/OsgiBroker.java b/artemis-server-osgi/src/main/java/org/apache/activemq/artemis/osgi/OsgiBroker.java index a4ecca5c8d..587fe7ec0a 100644 --- a/artemis-server-osgi/src/main/java/org/apache/activemq/artemis/osgi/OsgiBroker.java +++ b/artemis-server-osgi/src/main/java/org/apache/activemq/artemis/osgi/OsgiBroker.java @@ -36,43 +36,36 @@ import org.apache.activemq.artemis.core.server.ActiveMQComponent; import org.apache.activemq.artemis.core.server.ActiveMQServer; import org.apache.activemq.artemis.jms.server.config.impl.FileJMSConfiguration; import org.apache.activemq.artemis.spi.core.protocol.ProtocolManagerFactory; -import org.apache.activemq.artemis.spi.core.security.ActiveMQSecurityManager; +import org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; +import org.osgi.service.component.ComponentContext; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Deactivate; import org.osgi.util.tracker.ServiceTracker; @SuppressWarnings({"unchecked", "rawtypes"}) +@Component(configurationPid="org.apache.activemq.artemis") public class OsgiBroker { - private final String name; - - private final String configurationUrl; - - private final String brokerInstance; - - private boolean started; - - private final ActiveMQSecurityManager securityManager; - + private String name; + private String configurationUrl; private Map components; - private Map> registrations; - - private BundleContext context; - private ServiceTracker tracker; - public OsgiBroker(BundleContext context, String name, String brokerInstance, String configuration, ActiveMQSecurityManager security) { - this.context = context; - this.name = name; - this.brokerInstance = brokerInstance; - this.securityManager = security; - this.configurationUrl = configuration; - } - - - public synchronized void start() throws Exception { - if (tracker != null) { - return; + @Activate + public void activate(ComponentContext cctx) throws Exception { + BundleContext context = cctx.getBundleContext(); + Dictionary properties = cctx.getProperties(); + configurationUrl = getMandatory(properties, "config"); + name = getMandatory(properties, "name"); + String domain = getMandatory(properties, "domain"); + ActiveMQJAASSecurityManager security = new ActiveMQJAASSecurityManager(domain); + String brokerInstance = null; + String karafDataDir = System.getProperty("karaf.data"); + if (karafDataDir != null) { + brokerInstance = karafDataDir + "/artemis/" + name; } // todo if we start to pullout more configs from the main config then we @@ -86,7 +79,7 @@ public class OsgiBroker { FileDeploymentManager fileDeploymentManager = new FileDeploymentManager(configurationUrl); fileDeploymentManager.addDeployable(configuration).addDeployable(jmsConfiguration).readConfiguration(); - components = fileDeploymentManager.buildService(securityManager, ManagementFactory.getPlatformMBeanServer()); + components = fileDeploymentManager.buildService(security, ManagementFactory.getPlatformMBeanServer()); final ActiveMQServer server = (ActiveMQServer)components.get("core"); @@ -129,8 +122,16 @@ public class OsgiBroker { ProtocolTracker trackerCust = new ProtocolTracker(name, context, requiredProtocols, callback); tracker = new ServiceTracker(context, ProtocolManagerFactory.class, trackerCust); tracker.open(); - started = true; } + + private String getMandatory(Dictionary properties, String key) { + String value = (String) properties.get(key); + if (value == null) { + throw new IllegalStateException("Property " + key + " must be set"); + } + return value; + } + private String[] getRequiredProtocols(Set acceptors) { ArrayList protocols = new ArrayList(); @@ -143,16 +144,9 @@ public class OsgiBroker { return protocols.toArray(new String[]{}); } + @Deactivate public void stop() throws Exception { - if (!started) { - return; - } tracker.close(); - tracker = null; - } - - public boolean isStarted() { - return started; } public Map getComponents() { diff --git a/artemis-server-osgi/src/test/java/org/apache/activemq/artemis/osgi/ProtocolTrackerTest.java b/artemis-server-osgi/src/test/java/org/apache/activemq/artemis/osgi/ProtocolTrackerTest.java index 348ae2534a..5fd7d8b094 100644 --- a/artemis-server-osgi/src/test/java/org/apache/activemq/artemis/osgi/ProtocolTrackerTest.java +++ b/artemis-server-osgi/src/test/java/org/apache/activemq/artemis/osgi/ProtocolTrackerTest.java @@ -32,6 +32,7 @@ public class ProtocolTrackerTest { EasyMock.expectLastCall(); callback.removeFactory(protA.factory); + EasyMock.expectLastCall(); callback.stop(); EasyMock.expectLastCall();