Author: dblevins
Date: Thu Jan 7 18:53:52 2010
New Revision: 896964
URL: http://svn.apache.org/viewvc?rev=896964&view=rev
Log:
Patch from Jarek Gawor, OPENEJB-1117: ServiceManager does not work in OSGi
environment
Added:
openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/SimpleServiceManager.java
(with props)
openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/osgi/
openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/osgi/Activator.java
(with props)
openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/osgi/ServiceManagerExtender.java
(with props)
Modified:
openejb/trunk/openejb3/server/openejb-server/pom.xml
openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/ServiceManager.java
Modified: openejb/trunk/openejb3/server/openejb-server/pom.xml
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-server/pom.xml?rev=896964&r1=896963&r2=896964&view=diff
==============================================================================
--- openejb/trunk/openejb3/server/openejb-server/pom.xml (original)
+++ openejb/trunk/openejb3/server/openejb-server/pom.xml Thu Jan 7 18:53:52
2010
@@ -46,6 +46,15 @@
</excludes>
</configuration>
</plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <configuration>
+ <instructions>
+
<Bundle-Activator>org.apache.openejb.server.osgi.Activator</Bundle-Activator>
+ </instructions>
+ </configuration>
+ </plugin>
</plugins>
</build>
<dependencies>
@@ -86,6 +95,18 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ <version>1.4.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.osgi.compendium</artifactId>
+ <version>1.4.0</version>
+ <scope>provided</scope>
+ </dependency>
</dependencies>
</project>
Modified:
openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/ServiceManager.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/ServiceManager.java?rev=896964&r1=896963&r2=896964&view=diff
==============================================================================
---
openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/ServiceManager.java
(original)
+++
openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/ServiceManager.java
Thu Jan 7 18:53:52 2010
@@ -16,54 +16,41 @@
*/
package org.apache.openejb.server;
-import org.apache.openejb.loader.FileUtils;
-import org.apache.openejb.loader.SystemInstance;
-import org.apache.openejb.util.LogCategory;
-import org.apache.openejb.util.Logger;
-import org.apache.openejb.util.Messages;
-import org.apache.openejb.spi.ContainerSystem;
-import org.apache.openejb.assembler.classic.ServiceInfo;
-import org.apache.openejb.assembler.classic.OpenEjbConfiguration;
-import org.apache.xbean.finder.ResourceFinder;
-import org.apache.xbean.recipe.ObjectRecipe;
-import org.apache.xbean.recipe.Option;
-
-import javax.naming.NamingException;
-import javax.naming.Binding;
-import javax.naming.NamingEnumeration;
-import javax.resource.spi.ResourceAdapter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
-import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
+import org.apache.openejb.assembler.classic.OpenEjbConfiguration;
+import org.apache.openejb.assembler.classic.ServiceInfo;
+import org.apache.openejb.loader.FileUtils;
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.util.LogCategory;
+import org.apache.openejb.util.Logger;
+import org.apache.xbean.recipe.ObjectRecipe;
+import org.apache.xbean.recipe.Option;
+
/**
* @version $Rev$ $Date$
* @org.apache.xbean.XBean element="serviceManager"
*/
-public class ServiceManager {
+public abstract class ServiceManager {
- static Messages messages = new
Messages("org.apache.openejb.server.util.resources");
static Logger logger =
Logger.getInstance(LogCategory.OPENEJB_SERVER_REMOTE,
"org.apache.openejb.server.util.resources");
private static ServiceManager manager;
- private static ServerService[] daemons;
-
- private boolean stop = false;
-
public ServiceManager() {
}
public static ServiceManager getManager() {
if (manager == null) {
- manager = new ServiceManager();
+ manager = new SimpleServiceManager();
}
return manager;
@@ -73,155 +60,98 @@
return manager;
}
-
- // Have properties files (like xinet.d) that specifies what daemons to
- // Look into the xinet.d file structure again
- // conf/server.d/
- // admin.properties
- // ejbd.properties
- // webadmin.properties
- // telnet.properties
- // corba.properties
- // soap.properties
- // xmlrpc.properties
- // httpejb.properties
- // webejb.properties
- // xmlejb.properties
- // Each contains the class name of the daemon implamentation
- // The port to use
- // whether it's turned on
-
- // May be reusable elsewhere, move if another use occurs
- public static class ServiceFinder {
- private final ResourceFinder resourceFinder;
- private ClassLoader classLoader;
-
- public ServiceFinder(String basePath) {
- this(basePath, Thread.currentThread().getContextClassLoader());
- }
-
- public ServiceFinder(String basePath, ClassLoader classLoader) {
- this.resourceFinder = new ResourceFinder(basePath, classLoader);
- this.classLoader = classLoader;
- }
-
- public Map mapAvailableServices(Class interfase) throws IOException,
ClassNotFoundException {
- Map services =
resourceFinder.mapAvailableProperties(ServerService.class.getName());
-
- for (Iterator iterator = services.entrySet().iterator();
iterator.hasNext();) {
- Map.Entry entry = (Map.Entry) iterator.next();
- String name = (String) entry.getKey();
- Properties properties = (Properties) entry.getValue();
-
- String className = properties.getProperty("className");
- if (className == null) {
- className = properties.getProperty("classname");
- if (className == null) {
- className = properties.getProperty("server");
- }
- }
-
- Class impl = classLoader.loadClass(className);
-
- properties.put(interfase, impl);
- String rawProperties =
resourceFinder.findString(interfase.getName() + "/" + name);
- properties.put(Properties.class, rawProperties);
-
- }
- return services;
- }
+ protected static void setServiceManager(ServiceManager newManager) {
+ manager = newManager;
}
+
+ protected List<ServerService> initServers(Map<String, Properties>
availableServices)
+ throws IOException {
+ List<ServerService> enabledServers = new ArrayList<ServerService>();
- public void init() throws Exception {
- try {
- org.apache.log4j.MDC.put("SERVER", "main");
- InetAddress localhost = InetAddress.getLocalHost();
- org.apache.log4j.MDC.put("HOST", localhost.getHostAddress());
- } catch (Throwable e) {
- }
-
- DiscoveryRegistry registry = new DiscoveryRegistry();
- SystemInstance.get().setComponent(DiscoveryRegistry.class, registry);
-
- ServiceFinder serviceFinder = new ServiceFinder("META-INF/");
-
- Map<String, Properties> availableServices =
serviceFinder.mapAvailableServices(ServerService.class);
-
- List enabledServers = new ArrayList();
-
- OpenEjbConfiguration conf =
SystemInstance.get().getComponent(OpenEjbConfiguration.class);
for (Iterator iterator = availableServices.entrySet().iterator();
iterator.hasNext();) {
Map.Entry entry = (Map.Entry) iterator.next();
String serviceName = (String) entry.getKey();
Properties serviceProperties = (Properties) entry.getValue();
-
- logger.debug("Processing ServerService(id="+serviceName+")");
-
- overrideProperties(serviceName, serviceProperties);
- serviceProperties.setProperty("name", serviceName);
-
- if (conf != null && conf.facilities != null){
- ServiceInfo info = new ServiceInfo();
- info.className = ((Class)
serviceProperties.get(ServerService.class)).getName();
- info.service = "ServerService";
- info.id = serviceName;
- info.properties = serviceProperties;
- conf.facilities.services.add(info);
+
+ ServerService service = initServer(serviceName, serviceProperties);
+ if (service != null) {
+ enabledServers.add(service);
}
+ }
+
+ return enabledServers;
+ }
+
+ protected ServerService initServer(String serviceName, Properties
serviceProperties)
+ throws IOException {
+ DiscoveryRegistry registry =
SystemInstance.get().getComponent(DiscoveryRegistry.class);
+
+ OpenEjbConfiguration conf =
SystemInstance.get().getComponent(OpenEjbConfiguration.class);
+
+ logger.debug("Processing ServerService(id="+serviceName+")");
+ overrideProperties(serviceName, serviceProperties);
+ serviceProperties.setProperty("name", serviceName);
- boolean enabled = isEnabled(serviceProperties);
+ if (conf != null && conf.facilities != null){
+ ServiceInfo info = new ServiceInfo();
+ info.className = ((Class)
serviceProperties.get(ServerService.class)).getName();
+ info.service = "ServerService";
+ info.id = serviceName;
+ info.properties = serviceProperties;
+ conf.facilities.services.add(info);
+ }
- logger.debug("Found ServerService(id=" + serviceName + ",
disabled=" + (!enabled) + ")");
+ boolean enabled = isEnabled(serviceProperties);
- if (enabled) {
+ logger.debug("Found ServerService(id=" + serviceName + ", disabled=" +
(!enabled) + ")");
- Class serviceClass = (Class)
serviceProperties.get(ServerService.class);
+ if (enabled) {
- logger.debug("Creating ServerService(id=" + serviceName + ")");
+ Class serviceClass = (Class)
serviceProperties.get(ServerService.class);
- try {
- // Create Service
- ServerService service = null;
+ logger.debug("Creating ServerService(id=" + serviceName + ")");
+ try {
+ // Create Service
+ ServerService service = null;
- ObjectRecipe recipe = new ObjectRecipe(serviceClass);
- try {
- // Do not import. This class is not available in
xbean-reflect-3.3
- if
(org.apache.xbean.recipe.ReflectionUtil.findStaticFactory(serviceClass,
"createServerService", null, null) != null){
- recipe = new ObjectRecipe(serviceClass,
"createServerService");
- }
- } catch (Throwable e) {
+ ObjectRecipe recipe = new ObjectRecipe(serviceClass);
+ try {
+ // Do not import. This class is not available in
xbean-reflect-3.3
+ if
(org.apache.xbean.recipe.ReflectionUtil.findStaticFactory(serviceClass,
"createServerService", null, null) != null){
+ recipe = new ObjectRecipe(serviceClass,
"createServerService");
}
+ } catch (Throwable e) {
+ }
- recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
- recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
-
- service = (ServerService)
recipe.create(serviceClass.getClassLoader());
+ recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
+ recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
- if (service instanceof DiscoveryAgent){
- DiscoveryAgent agent = (DiscoveryAgent) service;
- registry.addDiscoveryAgent(agent);
- }
+ service = (ServerService)
recipe.create(serviceClass.getClassLoader());
- if (!(service instanceof SelfManaging)) {
- service = new ServicePool(service, serviceName,
serviceProperties);
- service = new ServiceLogger(service);
- service = new ServiceAccessController(service);
- service = new ServiceDaemon(service);
- }
+ if (service instanceof DiscoveryAgent){
+ DiscoveryAgent agent = (DiscoveryAgent) service;
+ registry.addDiscoveryAgent(agent);
+ }
- service.init(serviceProperties);
- enabledServers.add(service);
- } catch (Throwable t) {
- logger.error("service.instantiation.err", t,
serviceClass.getName(), t.getClass().getName(), t.getMessage());
+ if (!(service instanceof SelfManaging)) {
+ service = new ServicePool(service, serviceName,
serviceProperties);
+ service = new ServiceLogger(service);
+ service = new ServiceAccessController(service);
+ service = new ServiceDaemon(service);
}
- }
+ service.init(serviceProperties);
+
+ return service;
+ } catch (Throwable t) {
+ logger.error("service.instantiation.err", t,
serviceClass.getName(), t.getClass().getName(), t.getMessage());
+ }
}
-
- daemons = (ServerService[]) enabledServers.toArray(new
ServerService[]{});
+
+ return null;
}
private void overrideProperties(String serviceName, Properties
serviceProperties) throws IOException {
@@ -276,110 +206,14 @@
}
}
- public synchronized void start() throws ServiceException {
+ abstract public void init() throws Exception;
+
+ public void start() throws ServiceException {
start(true);
}
- public synchronized void start(boolean block) throws ServiceException {
- boolean display = System.getProperty("openejb.nobanner") == null;
-
- if (display) {
- System.out.println(" ** Starting Services **");
- printRow("NAME", "IP", "PORT");
- }
-
- for (int i = 0; i < daemons.length; i++) {
- ServerService d = daemons[i];
- try {
- d.start();
- if (display && d.getPort() != -1) {
- printRow(d.getName(), d.getIP(), d.getPort() + "");
- }
- } catch (Exception e) {
- logger.fatal("Service Start Failed: "+d.getName() + " " +
d.getIP() + " " + d.getPort() + ": " + e.getMessage());
- if (display) {
- printRow(d.getName(), "----", "FAILED");
- }
- }
- }
- if (display) {
- System.out.println("-------");
- System.out.println("Ready!");
- }
- if (!block) return;
-
- /*
- * This will cause the user thread (the thread that keeps the
- * vm alive) to go into a state of constant waiting.
- * Each time the thread is woken up, it checks to see if
- * it should continue waiting.
- *
- * To stop the thread (and the VM), just call the stop method
- * which will set 'stop' to true and notify the user thread.
- */
- try {
- while (!stop) {
+ abstract public void start(boolean block) throws ServiceException;
- this.wait(Long.MAX_VALUE);
- }
- } catch (Throwable t) {
- logger.fatal("Unable to keep the server thread alive. Received
exception: " + t.getClass().getName() + " : " + t.getMessage());
- }
- logger.info("Stopping Remote Server");
- }
-
- public synchronized void stop() throws ServiceException {
- logger.info("Received stop signal");
- stop = true;
-
- try {
- ContainerSystem containerSystem =
SystemInstance.get().getComponent(ContainerSystem.class);
- NamingEnumeration<Binding> namingEnumeration = null;
- try {
- namingEnumeration =
containerSystem.getJNDIContext().listBindings("openejb/resourceAdapter");
- } catch (NamingException ignored) {
- // no resource adapters were created
- }
- while (namingEnumeration != null &&
namingEnumeration.hasMoreElements()) {
- Binding binding = namingEnumeration.nextElement();
- Object object = binding.getObject();
- ResourceAdapter resourceAdapter = (ResourceAdapter) object;
- try {
- resourceAdapter.stop();
- } catch (Exception e) {
- logger.fatal("ResourceAdapter Shutdown Failed:
"+binding.getName(), e);
- }
- }
- } catch (Throwable e) {
- logger.fatal("Unable to get ResourceAdapters from JNDI. Stop must
be called on them for proper vm shutdown.", e);
- }
-
- for (int i = 0; i < daemons.length; i++) {
- try {
- daemons[i].stop();
- } catch (ServiceException e) {
- logger.fatal("Service Shutdown Failed:
"+daemons[i].getName()+". Exception: "+e.getMessage(), e);
- }
- }
- notifyAll();
- }
-
- private void printRow(String col1, String col2, String col3) {
-
- col1 += " ";
- col1 = col1.substring(0, 20);
-
- col2 += " ";
- col2 = col2.substring(0, 15);
-
- col3 += " ";
- col3 = col3.substring(0, 6);
-
- StringBuffer sb = new StringBuffer(50);
- sb.append(" ").append(col1);
- sb.append(" ").append(col2);
- sb.append(" ").append(col3);
-
- System.out.println(sb.toString());
- }
-}
+ abstract public void stop() throws ServiceException;
+
+}
\ No newline at end of file
Added:
openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/SimpleServiceManager.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/SimpleServiceManager.java?rev=896964&view=auto
==============================================================================
---
openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/SimpleServiceManager.java
(added)
+++
openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/SimpleServiceManager.java
Thu Jan 7 18:53:52 2010
@@ -0,0 +1,231 @@
+/**
+ * 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.openejb.server;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.naming.Binding;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.resource.spi.ResourceAdapter;
+
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.spi.ContainerSystem;
+import org.apache.xbean.finder.ResourceFinder;
+
+/**
+ * @version $Rev$ $Date$
+ * @org.apache.xbean.XBean element="simpleServiceManager"
+ */
+public class SimpleServiceManager extends ServiceManager {
+
+ private ServerService[] daemons;
+
+ private boolean stop = false;
+
+ public SimpleServiceManager() {
+ }
+
+ // Have properties files (like xinet.d) that specifies what daemons to
+ // Look into the xinet.d file structure again
+ // conf/server.d/
+ // admin.properties
+ // ejbd.properties
+ // webadmin.properties
+ // telnet.properties
+ // corba.properties
+ // soap.properties
+ // xmlrpc.properties
+ // httpejb.properties
+ // webejb.properties
+ // xmlejb.properties
+ // Each contains the class name of the daemon implamentation
+ // The port to use
+ // whether it's turned on
+
+ // May be reusable elsewhere, move if another use occurs
+ public static class ServiceFinder {
+ private final ResourceFinder resourceFinder;
+ private ClassLoader classLoader;
+
+ public ServiceFinder(String basePath) {
+ this(basePath, Thread.currentThread().getContextClassLoader());
+ }
+
+ public ServiceFinder(String basePath, ClassLoader classLoader) {
+ this.resourceFinder = new ResourceFinder(basePath, classLoader);
+ this.classLoader = classLoader;
+ }
+
+ public Map mapAvailableServices(Class interfase) throws IOException,
ClassNotFoundException {
+ Map services =
resourceFinder.mapAvailableProperties(ServerService.class.getName());
+
+ for (Iterator iterator = services.entrySet().iterator();
iterator.hasNext();) {
+ Map.Entry entry = (Map.Entry) iterator.next();
+ String name = (String) entry.getKey();
+ Properties properties = (Properties) entry.getValue();
+
+ String className = properties.getProperty("className");
+ if (className == null) {
+ className = properties.getProperty("classname");
+ if (className == null) {
+ className = properties.getProperty("server");
+ }
+ }
+
+ Class impl = classLoader.loadClass(className);
+
+ properties.put(interfase, impl);
+ String rawProperties =
resourceFinder.findString(interfase.getName() + "/" + name);
+ properties.put(Properties.class, rawProperties);
+
+ }
+ return services;
+ }
+ }
+
+ @Override
+ public void init() throws Exception {
+ try {
+ org.apache.log4j.MDC.put("SERVER", "main");
+ InetAddress localhost = InetAddress.getLocalHost();
+ org.apache.log4j.MDC.put("HOST", localhost.getHostName());
+ } catch (Throwable e) {
+ }
+
+ DiscoveryRegistry registry = new DiscoveryRegistry();
+ SystemInstance.get().setComponent(DiscoveryRegistry.class, registry);
+
+ ServiceFinder serviceFinder = new ServiceFinder("META-INF/");
+
+ Map<String, Properties> availableServices =
serviceFinder.mapAvailableServices(ServerService.class);
+
+ List<ServerService> enabledServers = initServers(availableServices);
+
+ daemons = (ServerService[]) enabledServers.toArray(new
ServerService[]{});
+ }
+
+ @Override
+ public synchronized void start(boolean block) throws ServiceException {
+ boolean display = System.getProperty("openejb.nobanner") == null;
+
+ if (display) {
+ System.out.println(" ** Starting Services **");
+ printRow("NAME", "IP", "PORT");
+ }
+
+ for (int i = 0; i < daemons.length; i++) {
+ ServerService d = daemons[i];
+ try {
+ d.start();
+ if (display && d.getPort() != -1) {
+ printRow(d.getName(), d.getIP(), d.getPort() + "");
+ }
+ } catch (Exception e) {
+ logger.fatal("Service Start Failed: "+d.getName() + " " +
d.getIP() + " " + d.getPort() + ": " + e.getMessage());
+ if (display) {
+ printRow(d.getName(), "----", "FAILED");
+ }
+ }
+ }
+ if (display) {
+ System.out.println("-------");
+ System.out.println("Ready!");
+ }
+ if (!block) return;
+
+ /*
+ * This will cause the user thread (the thread that keeps the
+ * vm alive) to go into a state of constant waiting.
+ * Each time the thread is woken up, it checks to see if
+ * it should continue waiting.
+ *
+ * To stop the thread (and the VM), just call the stop method
+ * which will set 'stop' to true and notify the user thread.
+ */
+ try {
+ while (!stop) {
+
+ this.wait(Long.MAX_VALUE);
+ }
+ } catch (Throwable t) {
+ logger.fatal("Unable to keep the server thread alive. Received
exception: " + t.getClass().getName() + " : " + t.getMessage());
+ }
+ logger.info("Stopping Remote Server");
+ }
+
+ @Override
+ public synchronized void stop() throws ServiceException {
+ logger.info("Received stop signal");
+ stop = true;
+
+ try {
+ ContainerSystem containerSystem =
SystemInstance.get().getComponent(ContainerSystem.class);
+ NamingEnumeration<Binding> namingEnumeration = null;
+ try {
+ namingEnumeration =
containerSystem.getJNDIContext().listBindings("openejb/resourceAdapter");
+ } catch (NamingException ignored) {
+ // no resource adapters were created
+ }
+ while (namingEnumeration != null &&
namingEnumeration.hasMoreElements()) {
+ Binding binding = namingEnumeration.nextElement();
+ Object object = binding.getObject();
+ ResourceAdapter resourceAdapter = (ResourceAdapter) object;
+ try {
+ resourceAdapter.stop();
+ } catch (Exception e) {
+ logger.fatal("ResourceAdapter Shutdown Failed:
"+binding.getName(), e);
+ }
+ }
+ } catch (Throwable e) {
+ logger.fatal("Unable to get ResourceAdapters from JNDI. Stop must
be called on them for proper vm shutdown.", e);
+ }
+
+ for (int i = 0; i < daemons.length; i++) {
+ try {
+ daemons[i].stop();
+ } catch (ServiceException e) {
+ logger.fatal("Service Shutdown Failed:
"+daemons[i].getName()+". Exception: "+e.getMessage(), e);
+ }
+ }
+ notifyAll();
+ }
+
+ private void printRow(String col1, String col2, String col3) {
+
+ col1 += " ";
+ col1 = col1.substring(0, 20);
+
+ col2 += " ";
+ col2 = col2.substring(0, 15);
+
+ col3 += " ";
+ col3 = col3.substring(0, 6);
+
+ StringBuffer sb = new StringBuffer(50);
+ sb.append(" ").append(col1);
+ sb.append(" ").append(col2);
+ sb.append(" ").append(col3);
+
+ System.out.println(sb.toString());
+ }
+}
Propchange:
openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/SimpleServiceManager.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/osgi/Activator.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/osgi/Activator.java?rev=896964&view=auto
==============================================================================
---
openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/osgi/Activator.java
(added)
+++
openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/osgi/Activator.java
Thu Jan 7 18:53:52 2010
@@ -0,0 +1,46 @@
+/**
+ * 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.openejb.server.osgi;
+
+import org.apache.openejb.server.ServiceManager;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class Activator implements BundleActivator {
+
+ private ServiceManagerExtender manager;
+ private ServiceRegistration registration;
+
+ public void start(BundleContext bundleContext) throws Exception {
+ manager = new ServiceManagerExtender(bundleContext);
+ registration =
bundleContext.registerService(ServiceManager.class.getName(), manager, null);
+ }
+
+ public void stop(BundleContext bundleContext) throws Exception {
+ if (manager != null) {
+ manager.shutdown();
+ }
+ if (registration != null) {
+ registration.unregister();
+ }
+ }
+
+}
Propchange:
openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/osgi/Activator.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/osgi/ServiceManagerExtender.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/osgi/ServiceManagerExtender.java?rev=896964&view=auto
==============================================================================
---
openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/osgi/ServiceManagerExtender.java
(added)
+++
openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/osgi/ServiceManagerExtender.java
Thu Jan 7 18:53:52 2010
@@ -0,0 +1,289 @@
+/**
+ * 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.openejb.server.osgi;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.server.DiscoveryRegistry;
+import org.apache.openejb.server.ServerService;
+import org.apache.openejb.server.ServiceManager;
+import org.apache.openejb.util.LogCategory;
+import org.apache.openejb.util.Logger;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.util.tracker.BundleTracker;
+import org.osgi.util.tracker.BundleTrackerCustomizer;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ServiceManagerExtender extends ServiceManager {
+
+ static Logger logger =
Logger.getInstance(LogCategory.OPENEJB_SERVER_REMOTE,
"org.apache.openejb.server.util.resources");
+
+ private BundleContext bundleContext;
+ private BundleTracker tracker;
+ private Map<Bundle, List<Service>> serverMap = new HashMap<Bundle,
List<Service>>();
+ private Boolean started;
+
+ public ServiceManagerExtender(BundleContext bundleContext) {
+ setServiceManager(this);
+
+ this.bundleContext = bundleContext;
+ }
+
+ public void init() throws Exception {
+ if (started != null) {
+ throw new IllegalStateException("ServiceManager is already
initialized");
+ }
+ DiscoveryRegistry registry = new DiscoveryRegistry();
+ SystemInstance.get().setComponent(DiscoveryRegistry.class, registry);
+
+ started = Boolean.FALSE;
+ ServerServiceTracker t = new ServerServiceTracker();
+ tracker = new BundleTracker(bundleContext, Bundle.ACTIVE |
Bundle.STOPPING, t);
+ tracker.open();
+ }
+
+ public synchronized void start(boolean block) {
+ if (started == null) {
+ throw new IllegalStateException("ServiceManager not initialized");
+ }
+ started = Boolean.TRUE;
+ for (Map.Entry<Bundle, List<Service>> entry : serverMap.entrySet()) {
+ for (Service service : entry.getValue()) {
+ service.start();
+ }
+ }
+ }
+
+ private synchronized void startServers(Bundle bundle, List<Service>
services) {
+ serverMap.put(bundle, services);
+ if (started == Boolean.TRUE) {
+ for (Service service : services) {
+ service.start();
+ }
+ }
+ }
+
+ protected void addedServers(Bundle bundle, Map<String, Properties>
resources) {
+ List<Service> services = new ArrayList<Service>();
+ for (Map.Entry<String, Properties> entry : resources.entrySet()) {
+ services.add(new Service(bundle, entry.getKey(),
entry.getValue()));
+ }
+ startServers(bundle, services);
+ }
+
+ public synchronized void stop() {
+ if (started == Boolean.TRUE) {
+ started = Boolean.FALSE;
+ for (Map.Entry<Bundle, List<Service>> entry :
serverMap.entrySet()) {
+ for (Service service : entry.getValue()) {
+ service.stop();
+ }
+ }
+ }
+ }
+
+ protected synchronized void removedServers(Bundle bundle) {
+ List<Service> services = serverMap.remove(bundle);
+ if (services != null) {
+ for (Service service : services) {
+ service.stop();
+ }
+ }
+ }
+
+ protected void shutdown() {
+ if (tracker != null) {
+ tracker.close();
+ }
+ stop();
+ }
+
+ private class Service {
+
+ private String name;
+ private Properties description;
+ private Bundle bundle;
+
+ private ServiceRegistration registration;
+ private ServerService server;
+
+ public Service(Bundle bundle, String name, Properties description) {
+ this.bundle = bundle;
+ this.name = name;
+ this.description = description;
+ }
+
+ public void start() {
+ try {
+ server = initServer(name, description);
+ } catch (IOException e) {
+ logger.error("Error initializing " + name + " service.", e);
+ }
+
+ if (server != null) {
+ try {
+ server.start();
+ } catch (Exception e) {
+ logger.error("Service Start Failed: " + name + " " +
server.getIP() + " " + server.getPort() + ". Exception: " + e.getMessage(), e);
+ }
+
+ BundleContext context = bundle.getBundleContext();
+ registration =
context.registerService(ServerService.class.getName(),
+ server,
+ getServiceProperties());
+ }
+ }
+
+ private Hashtable getServiceProperties() {
+ Hashtable props = new Hashtable();
+ for (Map.Entry<Object, Object> entry : description.entrySet()) {
+ if (entry.getKey() instanceof String) {
+ props.put(entry.getKey(), entry.getValue());
+ }
+ }
+ return props;
+ }
+
+ public void stop() {
+ if (server != null) {
+ try {
+ server.stop();
+ } catch (Exception e) {
+ logger.warning("Service Shutdown Failed: " + name + ".
Exception: " + e.getMessage(), e);
+ }
+ }
+ if (registration != null) {
+ try { registration.unregister(); } catch
(IllegalStateException ignore) {}
+ }
+ }
+ }
+
+ private class ServerServiceTracker implements BundleTrackerCustomizer {
+
+ public Object addingBundle(Bundle bundle, BundleEvent event) {
+ return scan(bundle);
+ }
+
+ public void modifiedBundle(Bundle bundle, BundleEvent event, Object
arg2) {
+ }
+
+ public void removedBundle(Bundle bundle, BundleEvent event, Object
arg2) {
+ removedServers(bundle);
+ }
+
+ private Object scan(Bundle bundle) {
+ String basePath = "/META-INF/" + ServerService.class.getName() +
"/";
+ Enumeration<URL> entries = bundle.findEntries(basePath, "*",
false);
+ if (entries != null) {
+ Map<String, Properties> resources = new HashMap<String,
Properties>();
+ while (entries.hasMoreElements()) {
+ URL entry = entries.nextElement();
+ String name = entry.getPath().substring(basePath.length());
+ try {
+ Properties props = loadProperties(entry);
+ setClass(props, bundle, ServerService.class);
+ setRawProperties(props, entry);
+ resources.put(name, props);
+ } catch (Exception e) {
+ logger.error("Error loading " + name + " properties",
e);
+ }
+ }
+ addedServers(bundle, resources);
+ return bundle;
+ }
+ return null;
+ }
+
+ private void setClass(Properties properties, Bundle bundle, Class
interfase) throws ClassNotFoundException {
+ String className = properties.getProperty("className");
+ if (className == null) {
+ className = properties.getProperty("classname");
+ if (className == null) {
+ className = properties.getProperty("server");
+ }
+ }
+
+ Class impl = bundle.loadClass(className);
+ properties.put(interfase, impl);
+ }
+
+ private void setRawProperties(Properties properties, URL entry) throws
IOException {
+ String rawProperties = readContents(entry);
+ properties.put(Properties.class, rawProperties);
+ }
+
+ private Properties loadProperties(URL resource) throws IOException {
+ InputStream in = resource.openStream();
+
+ BufferedInputStream reader = null;
+ try {
+ reader = new BufferedInputStream(in);
+ Properties properties = new Properties();
+ properties.load(reader);
+
+ return properties;
+ } finally {
+ try {
+ in.close();
+ reader.close();
+ } catch (Exception e) {
+ }
+ }
+ }
+
+ private String readContents(URL resource) throws IOException {
+ InputStream in = resource.openStream();
+ BufferedInputStream reader = null;
+ StringBuffer sb = new StringBuffer();
+
+ try {
+ reader = new BufferedInputStream(in);
+
+ int b = reader.read();
+ while (b != -1) {
+ sb.append((char) b);
+ b = reader.read();
+ }
+
+ return sb.toString().trim();
+ } finally {
+ try {
+ in.close();
+ reader.close();
+ } catch (Exception e) {
+ }
+ }
+ }
+
+ }
+}
Propchange:
openejb/trunk/openejb3/server/openejb-server/src/main/java/org/apache/openejb/server/osgi/ServiceManagerExtender.java
------------------------------------------------------------------------------
svn:eol-style = native