001/*
002 * (C) Copyright 2006-2014 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 *     tdelprat, jcarsique
019 */
020
021package org.nuxeo.ecm.admin.runtime;
022
023import java.io.File;
024import java.io.IOException;
025import java.io.InputStream;
026import java.util.ArrayList;
027import java.util.Collection;
028import java.util.Collections;
029import java.util.Enumeration;
030import java.util.List;
031import java.util.PropertyResourceBundle;
032import java.util.zip.ZipEntry;
033import java.util.zip.ZipFile;
034
035import org.apache.commons.logging.Log;
036import org.apache.commons.logging.LogFactory;
037import org.nuxeo.connect.update.Version;
038import org.nuxeo.osgi.BundleFile;
039import org.nuxeo.osgi.BundleImpl;
040import org.nuxeo.osgi.JarBundleFile;
041import org.nuxeo.runtime.RuntimeService;
042import org.nuxeo.runtime.api.Framework;
043import org.nuxeo.runtime.model.RegistrationInfo;
044import org.osgi.framework.Bundle;
045
046/**
047 * Extracts information about the Bundles currently deployed in Nuxeo Runtime
048 *
049 * @author tiry
050 */
051public class RuntimeInstrospection {
052
053    protected static final Log log = LogFactory.getLog(RuntimeInstrospection.class);
054
055    protected static SimplifiedServerInfo info;
056
057    protected static List<String> bundleIds;
058
059    public static synchronized SimplifiedServerInfo getInfo() {
060        if (info == null) {
061            RuntimeService runtime = Framework.getRuntime();
062            Collection<RegistrationInfo> registrations = runtime.getComponentManager().getRegistrations();
063            bundleIds = new ArrayList<>();
064            List<SimplifiedBundleInfo> bundles = new ArrayList<>();
065            for (RegistrationInfo ri : registrations) {
066                Bundle bundle = ri.getContext().getBundle();
067                if (bundle != null && !bundleIds.contains(bundle.getSymbolicName())) {
068                    SimplifiedBundleInfo bi = getBundleSimplifiedInfo(bundle);
069                    bundleIds.add(bundle.getSymbolicName());
070                    if (bi != null) {
071                        bundles.add(bi);
072                    }
073                }
074            }
075            Collections.sort(bundles);
076            info = new SimplifiedServerInfo();
077            info.setBundleInfos(bundles);
078            info.setRuntimeVersion(runtime.getVersion().toString());
079            info.setWarnings(runtime.getWarnings());
080        }
081        return info;
082    }
083
084    public static List<String> getBundleIds() {
085        if (bundleIds == null) {
086            getInfo();
087        }
088        return bundleIds;
089    }
090
091    protected static SimplifiedBundleInfo getBundleSimplifiedInfo(Bundle bundle) {
092        if (!(bundle instanceof BundleImpl)) {
093            return null;
094        }
095        BundleImpl nxBundle = (BundleImpl) bundle;
096        BundleFile file = nxBundle.getBundleFile();
097        File jarFile = null;
098        if (file instanceof JarBundleFile) {
099            JarBundleFile jar = (JarBundleFile) file;
100            jarFile = jar.getFile();
101        }
102        if (jarFile == null || jarFile.isDirectory()) {
103            return null;
104        }
105        SimplifiedBundleInfo result = null;
106        try (ZipFile zFile = new ZipFile(jarFile)) {
107            // Look for a pom.properties to extract its Maven version
108            Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zFile.entries();
109            while (entries.hasMoreElements()) {
110                ZipEntry entry = entries.nextElement();
111                if (entry.getName().endsWith("pom.properties")) {
112                    try (InputStream pomStream = zFile.getInputStream(entry)) {
113                        PropertyResourceBundle prb = new PropertyResourceBundle(pomStream);
114                        String version = prb.getString("version");
115                        result = new SimplifiedBundleInfo(bundle.getSymbolicName(), version);
116                    }
117                    break;
118                }
119            }
120        } catch (IOException e) {
121            log.debug(e.getMessage(), e);
122        }
123        if (result == null) {
124            // Fall back on the filename to extract a version
125            try {
126                Version version = new Version(jarFile.getName());
127                result = new SimplifiedBundleInfo(bundle.getSymbolicName(), version.toString());
128            } catch (NumberFormatException e) {
129                log.debug(e.getMessage());
130            }
131        }
132        if (result == null) {
133            // Fall back on the MANIFEST Bundle-Version
134            try {
135                org.osgi.framework.Version version = bundle.getVersion();
136                result = new SimplifiedBundleInfo(bundle.getSymbolicName(), version.toString());
137            } catch (RuntimeException e) {
138                log.debug(e.getMessage());
139                result = new SimplifiedBundleInfo(bundle.getSymbolicName(), "unknown");
140            }
141        }
142        return result;
143    }
144}