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.nuxeo.osgi.BundleFile; 029import org.nuxeo.osgi.BundleImpl; 030import org.nuxeo.osgi.OSGiAdapter; 031import org.osgi.framework.BundleException; 032 033/** 034 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 035 */ 036public class StandaloneBundleLoader extends ApplicationLoader { 037 038 protected SharedClassLoader loader; 039 040 public StandaloneBundleLoader(OSGiAdapter osgi) { 041 this(osgi, StandaloneBundleLoader.class.getClassLoader()); 042 } 043 044 public StandaloneBundleLoader(OSGiAdapter osgi, ClassLoader parentLoader) { 045 super(osgi); 046 loader = new SharedClassLoaderImpl(parentLoader); 047 } 048 049 public StandaloneBundleLoader(OSGiAdapter osgi, SharedClassLoader scl) { 050 super(osgi); 051 loader = scl; 052 } 053 054 public void setSharedClassLoader(SharedClassLoader loader) { 055 this.loader = loader; 056 } 057 058 public SharedClassLoader getSharedClassLoader() { 059 return loader; 060 } 061 062 @Override 063 public void installBundle(BundleFile bundleFile) throws BundleException { 064 osgi.install(new BundleImpl(osgi, bundleFile, loader.getLoader())); 065 } 066 067 @Override 068 public void loadBundle(BundleFile bundleFile) { 069 loader.addURL(bundleFile.getURL()); 070 } 071 072 @Override 073 public void loadJAR(BundleFile bundleFile) { 074 loader.addURL(bundleFile.getURL()); 075 } 076 077 public static void main(String[] args) throws Exception { 078 File home = new File("/tmp/test_osgi_loader"); 079 OSGiAdapter osgi = new OSGiAdapter(home); 080 System.out.println("Starting ..."); 081 StandaloneBundleLoader loader = new StandaloneBundleLoader(osgi); 082 Thread.currentThread().setContextClassLoader(loader.loader.getLoader()); 083 double s = System.currentTimeMillis(); 084 try { 085 loader.setExtractNestedJARs(true); 086 loader.setScanForNestedJARs(true); 087 List<BundleFile> bundles = new ArrayList<>(); 088 List<BundleFile> jars = new ArrayList<>(); 089 loader.load(new File("/opt/jboss/server/default/deploy/nuxeo.ear"), bundles, jars); 090 loader.installAll(bundles); 091 System.out.println(">>>> Loading done!!!!"); 092 } finally { 093 System.out.println("Shutting down"); 094 osgi.shutdown(); 095 } 096 double e = System.currentTimeMillis(); 097 System.out.println("Total time: " + ((e - s) / 1000) + " sec."); 098 System.exit(0); 099 } 100 101}