001/* 002 * (C) Copyright 2006-2017 Nuxeo (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 */ 020package org.nuxeo.ecm.admin.runtime; 021 022import java.io.File; 023import java.io.IOException; 024import java.io.InputStream; 025import java.util.ArrayList; 026import java.util.Collection; 027import java.util.Collections; 028import java.util.Enumeration; 029import java.util.List; 030import java.util.PropertyResourceBundle; 031import java.util.zip.ZipEntry; 032import java.util.zip.ZipFile; 033 034import org.apache.commons.logging.Log; 035import org.apache.commons.logging.LogFactory; 036import org.nuxeo.connect.update.Version; 037import org.nuxeo.osgi.BundleFile; 038import org.nuxeo.osgi.BundleImpl; 039import org.nuxeo.osgi.JarBundleFile; 040import org.nuxeo.runtime.RuntimeService; 041import org.nuxeo.runtime.api.Framework; 042import org.nuxeo.runtime.model.RegistrationInfo; 043import org.osgi.framework.Bundle; 044 045/** 046 * Extracts information about the Bundles currently deployed in Nuxeo Runtime 047 * 048 * @author tiry 049 */ 050public class RuntimeInstrospection { 051 052 protected static final Log log = LogFactory.getLog(RuntimeInstrospection.class); 053 054 protected static SimplifiedServerInfo info; 055 056 protected static List<String> bundleIds; 057 058 public static synchronized SimplifiedServerInfo getInfo() { 059 if (info == null) { 060 RuntimeService runtime = Framework.getRuntime(); 061 Collection<RegistrationInfo> registrations = runtime.getComponentManager().getRegistrations(); 062 bundleIds = new ArrayList<>(); 063 List<SimplifiedBundleInfo> bundles = new ArrayList<>(); 064 for (RegistrationInfo ri : registrations) { 065 Bundle bundle = ri.getContext().getBundle(); 066 if (bundle != null && !bundleIds.contains(bundle.getSymbolicName())) { 067 SimplifiedBundleInfo bi = getBundleSimplifiedInfo(bundle); 068 bundleIds.add(bundle.getSymbolicName()); 069 if (bi != null) { 070 bundles.add(bi); 071 } 072 } 073 } 074 Collections.sort(bundles); 075 info = new SimplifiedServerInfo(); 076 info.setBundleInfos(bundles); 077 info.setRuntimeVersion(runtime.getVersion().toString()); 078 info.setWarnings(runtime.getWarnings()); 079 info.setErrors(runtime.getErrors()); 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}