[jboss-osgi-commits] JBoss-OSGI SVN: r91712 - in projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi: util and 1 other directory.
jboss-osgi-commits at lists.jboss.org
jboss-osgi-commits at lists.jboss.org
Tue Jul 28 11:01:15 EDT 2009
Author: thomas.diesler at jboss.com
Date: 2009-07-28 11:01:15 -0400 (Tue, 28 Jul 2009)
New Revision: 91712
Added:
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/framework/FrameworkLoader.java
projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/util/ServiceLoader.java
Log:
Add FrameworkLoader
Added: projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/framework/FrameworkLoader.java
===================================================================
--- projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/framework/FrameworkLoader.java (rev 0)
+++ projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/framework/FrameworkLoader.java 2009-07-28 15:01:15 UTC (rev 91712)
@@ -0,0 +1,71 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.osgi.spi.framework;
+
+//$Id$
+
+import java.util.Map;
+
+import org.jboss.osgi.spi.util.ServiceLoader;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.launch.Framework;
+import org.osgi.framework.launch.FrameworkFactory;
+
+/**
+ * The FrameworkLoader uses the {@link org.osgi.framework.launch} API to load a new
+ * instance on a {@link Framework}.
+ *
+ * @author thomas.diesler at jboss.com
+ * @since 28-Jul-2009
+ */
+public abstract class FrameworkLoader
+{
+ /**
+ * Create a new {@link Framework} instance.
+ *
+ * @param configuration The framework properties to configure the new
+ * framework instance. If framework properties are not provided by
+ * the configuration argument, the created framework instance must
+ * use some reasonable default configuration appropriate for the
+ * current VM. For example, the system packages for the current
+ * execution environment should be properly exported. The specified
+ * configuration argument may be <code>null</code>. The created
+ * framework instance must copy any information needed from the
+ * specified configuration argument since the configuration argument
+ * can be changed after the framework instance has been created.
+ * @return A new, configured {@link Framework} instance. The framework
+ * instance must be in the {@link Bundle#INSTALLED} state.
+ * @throws SecurityException If the caller does not have
+ * <code>AllPermission</code>, and the Java Runtime Environment
+ * supports permissions.
+ */
+ @SuppressWarnings("unchecked")
+ public static Framework newFramework(Map configuration)
+ {
+ FrameworkFactory factory = ServiceLoader.loadService(FrameworkFactory.class);
+ if (factory == null)
+ throw new IllegalStateException("Cannot load FrameworkFactory");
+
+ return factory.newFramework(configuration);
+ }
+
+}
\ No newline at end of file
Property changes on: projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/framework/FrameworkLoader.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Added: projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/util/ServiceLoader.java
===================================================================
--- projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/util/ServiceLoader.java (rev 0)
+++ projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/util/ServiceLoader.java 2009-07-28 15:01:15 UTC (rev 91712)
@@ -0,0 +1,71 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.osgi.spi.util;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+/**
+ * Loads a service from the requesters classpath.
+ *
+ * @author Thomas.Diesler at jboss.com
+ * @since 14-Dec-2006
+ */
+public abstract class ServiceLoader
+{
+ /**
+ * Loads the requested service from META-INF/services/${serviceClass}
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T loadService(Class<T> serviceClass)
+ {
+ T factory = null;
+ String factoryName = null;
+ ClassLoader loader = serviceClass.getClassLoader();
+
+ // Use the Services API (as detailed in the JAR specification), if available, to determine the classname.
+ String filename = "META-INF/services/" + serviceClass.getName();
+ InputStream inStream = loader.getResourceAsStream(filename);
+ if (inStream != null)
+ {
+ try
+ {
+ BufferedReader br = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
+ factoryName = br.readLine();
+ br.close();
+ if (factoryName != null)
+ {
+ factoryName = factoryName.trim();
+ Class<T> factoryClass = (Class<T>)loader.loadClass(factoryName);
+ factory = factoryClass.newInstance();
+ }
+ }
+ catch (Throwable t)
+ {
+ throw new IllegalStateException("Failed to load " + serviceClass.getName() + ": " + factoryName, t);
+ }
+ }
+
+ return factory;
+ }
+}
Property changes on: projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/util/ServiceLoader.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
More information about the jboss-osgi-commits
mailing list