001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.osgi.application; 023 024import java.io.File; 025import java.util.ArrayList; 026import java.util.List; 027 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030import org.nuxeo.osgi.BundleFile; 031import org.nuxeo.osgi.BundleImpl; 032import org.nuxeo.osgi.OSGiAdapter; 033import org.osgi.framework.BundleException; 034 035/** 036 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 037 */ 038public class StandaloneBundleLoader extends ApplicationLoader { 039 040 private static final Log log = LogFactory.getLog(StandaloneBundleLoader.class); 041 042 protected SharedClassLoader loader; 043 044 public StandaloneBundleLoader(OSGiAdapter osgi) { 045 this(osgi, StandaloneBundleLoader.class.getClassLoader()); 046 } 047 048 public StandaloneBundleLoader(OSGiAdapter osgi, ClassLoader parentLoader) { 049 super(osgi); 050 loader = new SharedClassLoaderImpl(parentLoader); 051 } 052 053 public StandaloneBundleLoader(OSGiAdapter osgi, SharedClassLoader scl) { 054 super(osgi); 055 loader = scl; 056 } 057 058 public void setSharedClassLoader(SharedClassLoader loader) { 059 this.loader = loader; 060 } 061 062 public SharedClassLoader getSharedClassLoader() { 063 return loader; 064 } 065 066 @Override 067 public void installBundle(BundleFile bundleFile) throws BundleException { 068 osgi.install(new BundleImpl(osgi, bundleFile, loader.getLoader())); 069 } 070 071 @Override 072 public void loadBundle(BundleFile bundleFile) { 073 loader.addURL(bundleFile.getURL()); 074 } 075 076 @Override 077 public void loadJAR(BundleFile bundleFile) { 078 loader.addURL(bundleFile.getURL()); 079 } 080 081 public static void main(String[] args) throws Exception { 082 File home = new File("/tmp/test_osgi_loader"); 083 OSGiAdapter osgi = new OSGiAdapter(home); 084 System.out.println("Starting ..."); 085 StandaloneBundleLoader loader = new StandaloneBundleLoader(osgi); 086 Thread.currentThread().setContextClassLoader(loader.loader.getLoader()); 087 double s = System.currentTimeMillis(); 088 try { 089 loader.setExtractNestedJARs(true); 090 loader.setScanForNestedJARs(true); 091 List<BundleFile> bundles = new ArrayList<BundleFile>(); 092 List<BundleFile> jars = new ArrayList<BundleFile>(); 093 loader.load(new File("/opt/jboss/server/default/deploy/nuxeo.ear"), bundles, jars); 094 loader.installAll(bundles); 095 System.out.println(">>>> Loading done!!!!"); 096 } finally { 097 System.out.println("Shutting down"); 098 osgi.shutdown(); 099 } 100 double e = System.currentTimeMillis(); 101 System.out.println("Total time: " + ((e - s) / 1000) + " sec."); 102 System.exit(0); 103 } 104 105}