001/* 002 * (C) Copyright 2006-2010 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 * bstefanescu 018 */ 019package org.nuxeo.ecm.webengine.app; 020 021import java.io.File; 022import java.io.IOException; 023import java.util.Map; 024 025import org.apache.commons.io.FileUtils; 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.nuxeo.common.utils.Path; 029import org.nuxeo.common.utils.PathFilter; 030import org.nuxeo.common.utils.ZipUtils; 031import org.nuxeo.ecm.webengine.WebEngine; 032import org.nuxeo.osgi.BundleManifestReader; 033import org.nuxeo.runtime.api.Framework; 034import org.osgi.framework.Bundle; 035import org.osgi.framework.BundleContext; 036import org.osgi.framework.ServiceReference; 037import org.osgi.service.packageadmin.PackageAdmin; 038 039/** 040 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 041 */ 042public class WebEngineModuleFactory { 043 044 public static Log log = LogFactory.getLog(WebEngineModuleFactory.class); 045 046 public static Bundle[] getFragments(Bundle bundle) { 047 BundleContext context = bundle.getBundleContext(); 048 ServiceReference ref = context.getServiceReference(PackageAdmin.class.getName()); 049 PackageAdmin admin = (PackageAdmin) context.getService(ref); 050 return admin.getFragments(bundle); 051 } 052 053 public static WebEngineModule getApplication(WebEngineModule app, Bundle bundle, Map<String, String> attrs) 054 throws ReflectiveOperationException, IOException { 055 WebEngine engine = Framework.getService(WebEngine.class); 056 057 boolean explode = true; 058 if (attrs != null) { 059 if ("false".equals(attrs.get("explode"))) { 060 explode = false; 061 } 062 } 063 // register the web engine 064 065 File moduleDir = locateModuleDir(bundle, engine, explode); 066 067 app.init(engine, bundle, new File(moduleDir, "module.xml"), attrs); 068 069 app.cfg.directory = moduleDir; 070 071 Bundle[] fragments = getFragments(bundle); 072 for (Bundle fragment : fragments) { 073 File fragmentDir = locateModuleDir(fragment, engine, explode); 074 app.cfg.fragmentDirectories.add(fragmentDir); 075 } 076 app.cfg.allowHostOverride = Boolean.parseBoolean((String) bundle.getHeaders().get( 077 BundleManifestReader.ALLOW_HOST_OVERRIDE)); 078 engine.addApplication(app); 079 080 log.info("Deployed web module found in bundle: " + bundle.getSymbolicName()); 081 082 return app; 083 } 084 085 private static File locateModuleDir(Bundle bundle, WebEngine engine, boolean explode) throws IOException { 086 File moduleDir = null; 087 File bundleFile = Framework.getRuntime().getBundleFile(bundle); 088 if (explode) { 089 // this will also add the exploded directory to WebEngine class 090 // loader 091 moduleDir = explodeBundle(engine, bundle, bundleFile); 092 } else if (bundleFile.isDirectory()) { 093 moduleDir = bundleFile; 094 } 095 return moduleDir; 096 } 097 098 private static File explodeBundle(WebEngine engine, Bundle bundle, File bundleFile) throws IOException { 099 if (bundleFile.isDirectory()) { // exploded jar - deploy it as is. 100 return bundleFile; 101 } else { // should be a JAR - we copy the bundle module content 102 File moduleRoot = new File(engine.getRootDirectory(), "modules/" + bundle.getSymbolicName()); 103 if (moduleRoot.exists()) { 104 if (bundleFile.lastModified() < moduleRoot.lastModified()) { 105 // already deployed and JAR was not modified since. 106 return moduleRoot; 107 } 108 // remove existing files 109 FileUtils.deleteQuietly(moduleRoot); 110 } 111 // create the module root 112 moduleRoot.mkdirs(); 113 // avoid unziping classes 114 ZipUtils.unzip(bundleFile, moduleRoot, new PathFilter() { 115 @Override 116 public boolean isExclusive() { 117 return false; 118 } 119 120 @Override 121 public boolean accept(Path arg0) { 122 return !arg0.lastSegment().endsWith(".class"); 123 } 124 }); 125 return moduleRoot; 126 } 127 } 128 129}