001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.module.framework;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.util.ClassLoaderUtil;
021    import com.liferay.portal.util.PropsValues;
022    
023    import java.io.InputStream;
024    
025    import java.net.URL;
026    
027    import java.util.List;
028    import java.util.Map;
029    
030    /**
031     * This class is a simple wrapper in order to make the framework module running
032     * under its own class loader.
033     *
034     * @author Miguel Pastor
035     * @author Raymond Aug??
036     * @see    ModuleFrameworkClassLoader
037     */
038    public class ModuleFrameworkUtilAdapter {
039    
040            public static Object addBundle(String location) throws PortalException {
041                    return _moduleFramework.addBundle(location);
042            }
043    
044            public static Object addBundle(String location, InputStream inputStream)
045                    throws PortalException {
046    
047                    return _moduleFramework.addBundle(location, inputStream);
048            }
049    
050            public static Map<String, List<URL>> getExtraPackageMap() {
051                    return _moduleFramework.getExtraPackageMap();
052            }
053    
054            public static Object getFramework() {
055                    return _moduleFramework.getFramework();
056            }
057    
058            public static String getState(long bundleId) throws PortalException {
059                    return _moduleFramework.getState(bundleId);
060            }
061    
062            public static void registerContext(Object context) {
063                    _moduleFramework.registerContext(context);
064            }
065    
066            public static void setBundleStartLevel(long bundleId, int startLevel)
067                    throws PortalException {
068    
069                    _moduleFramework.setBundleStartLevel(bundleId, startLevel);
070            }
071    
072            public static void setModuleFramework(ModuleFramework moduleFramework) {
073                    _moduleFramework = moduleFramework;
074    
075                    _moduleFrameworkAdapterHelper.exec(
076                            "setModuleFramework", new Class[] {ModuleFramework.class},
077                            _moduleFramework);
078            }
079    
080            public static void startBundle(long bundleId) throws PortalException {
081                    _moduleFramework.startBundle(bundleId);
082            }
083    
084            public static void startBundle(long bundleId, int options)
085                    throws PortalException {
086    
087                    _moduleFramework.startBundle(bundleId, options);
088            }
089    
090            public static void startFramework() throws Exception {
091                    if (!PropsValues.MODULE_FRAMEWORK_ENABLED) {
092                            if (_log.isWarnEnabled()) {
093                                    _log.warn("Module framework is disabled");
094                            }
095    
096                            return;
097                    }
098    
099                    ClassLoader classLoader = ClassLoaderUtil.getContextClassLoader();
100    
101                    ClassLoaderUtil.setContextClassLoader(
102                            ModuleFrameworkAdapterHelper.getClassLoader());
103    
104                    try {
105                            _moduleFramework.startFramework();
106                    }
107                    finally {
108                            ClassLoaderUtil.setContextClassLoader(classLoader);
109                    }
110            }
111    
112            public static void startRuntime() throws Exception {
113                    _moduleFramework.startRuntime();
114            }
115    
116            public static void stopBundle(long bundleId) throws PortalException {
117                    _moduleFramework.stopBundle(bundleId);
118            }
119    
120            public static void stopBundle(long bundleId, int options)
121                    throws PortalException {
122    
123                    _moduleFramework.stopBundle(bundleId, options);
124            }
125    
126            public static void stopFramework() throws Exception {
127                    _moduleFramework.stopFramework();
128            }
129    
130            public static void stopRuntime() throws Exception {
131                    _moduleFramework.stopRuntime();
132            }
133    
134            public static void uninstallBundle(long bundleId) throws PortalException {
135                    _moduleFramework.uninstallBundle(bundleId);
136            }
137    
138            public static void updateBundle(long bundleId) throws PortalException {
139                    _moduleFramework.updateBundle(bundleId);
140            }
141    
142            public static void updateBundle(long bundleId, InputStream inputStream)
143                    throws PortalException {
144    
145                    _moduleFramework.updateBundle(bundleId, inputStream);
146            }
147    
148            private static Log _log = LogFactoryUtil.getLog(
149                    ModuleFrameworkUtilAdapter.class);
150    
151            private static ModuleFramework _moduleFramework;
152            private static ModuleFrameworkAdapterHelper _moduleFrameworkAdapterHelper =
153                    new ModuleFrameworkAdapterHelper(
154                            "com.liferay.osgi.bootstrap.ModuleFrameworkUtil");
155    
156            static {
157                    _moduleFramework =
158                            (ModuleFramework)_moduleFrameworkAdapterHelper.execute(
159                                    "getModuleFramework");
160            }
161    
162    }