📄 defaultbundlearchive.java
字号:
/* * Oscar - An implementation of the OSGi framework. * Copyright (c) 2004, Richard S. Hall * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of the ungoverned.org nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Contact: Richard S. Hall (heavy@ungoverned.org) * Contributor(s): ***/package org.ungoverned.oscar.util;import java.io.*;import java.security.AccessController;import java.security.PrivilegedActionException;import java.security.PrivilegedExceptionAction;import java.util.*;import java.util.jar.JarFile;import java.util.jar.Manifest;import java.util.zip.ZipEntry;import org.osgi.framework.Bundle;import org.osgi.framework.BundleActivator;import org.ungoverned.oscar.BundleArchive;import org.ungoverned.oscar.Oscar;/** * <p> * This class, combined with <tt>DefaultBundleCache</tt>, implements the * default file system-based bundle cache for Oscar. * </p> * @see org.ungoverned.oscar.util.DefaultBundleCache**/public class DefaultBundleArchive implements BundleArchive{ private static final transient String BUNDLE_JAR_FILE = "bundle.jar"; private static final transient String BUNDLE_LOCATION_FILE = "bundle.location"; private static final transient String BUNDLE_STATE_FILE = "bundle.state"; private static final transient String BUNDLE_START_LEVEL_FILE = "bundle.startlevel"; private static final transient String REFRESH_COUNTER_FILE = "refresh.counter"; private static final transient String BUNDLE_ACTIVATOR_FILE = "bundle.activator"; private static final transient String REVISION_DIRECTORY = "version"; private static final transient String EMBEDDED_DIRECTORY = "embedded"; private static final transient String LIBRARY_DIRECTORY = "lib"; private static final transient String DATA_DIRECTORY = "data"; private static final transient String ACTIVE_STATE = "active"; private static final transient String INSTALLED_STATE = "installed"; private static final transient String UNINSTALLED_STATE = "uninstalled"; private long m_id = -1; private File m_dir = null; private String m_location = null; private int m_persistentState = -1; private int m_startLevel = -1; private Map m_currentHeader = null; private long m_refreshCount = -1; private int m_revisionCount = -1; public DefaultBundleArchive(File dir, long id, String location, InputStream is) throws Exception { this(dir, id); m_location = location; // Try to save and pre-process the bundle JAR. try { initialize(is); } catch (Exception ex) { if (!deleteDirectoryTree(dir)) { Oscar.error("Unable to delete the archive directory."); } throw ex; } } public DefaultBundleArchive(File dir, long id) { m_dir = dir; m_id = id; if (m_id <= 0) { throw new IllegalArgumentException( "Bundle ID cannot be less than or equal to zero."); } } private void initialize(InputStream is) throws Exception { if (System.getSecurityManager() != null) { try { AccessController.doPrivileged( new PrivilegedAction( PrivilegedAction.INITIALIZE_ACTION, this, is)); } catch (PrivilegedActionException ex) { throw ((PrivilegedActionException) ex).getException(); } } else { initializeUnchecked(is); } } private void initializeUnchecked(InputStream is) throws Exception { FileWriter fw = null; BufferedWriter bw = null; try { // Create archive directory. if (!m_dir.mkdir()) { Oscar.error("DefaultBundleArchive: Unable to create archive directory."); throw new IOException("Unable to create archive directory."); } // Save location string. File file = new File(m_dir, BUNDLE_LOCATION_FILE); fw = new FileWriter(file); bw = new BufferedWriter(fw); bw.write(m_location, 0, m_location.length()); // Create version/revision directory for bundle JAR. // Since this is only called when the bundle JAR is // first saved, the update and revision will always // be "0.0" for the directory name. File revisionDir = new File(m_dir, REVISION_DIRECTORY + "0.0"); if (!revisionDir.mkdir()) { Oscar.error("DefaultBundleArchive: Unable to create revision directory."); throw new IOException("Unable to create revision directory."); } // Save the bundle jar file. file = new File(revisionDir, BUNDLE_JAR_FILE); copy(is, file); // This will always be revision zero. preprocessBundleJar(0, revisionDir); } finally { if (is != null) is.close(); if (bw != null) bw.close(); if (fw != null) fw.close(); } } public File getDirectory() { return m_dir; } public long getId() { return m_id; } public String getLocation() throws Exception { if (m_location != null) { return m_location; } else if (System.getSecurityManager() != null) { try { return (String) AccessController.doPrivileged( new PrivilegedAction( PrivilegedAction.GET_LOCATION_ACTION, this)); } catch (PrivilegedActionException ex) { throw ((PrivilegedActionException) ex).getException(); } } else { return getLocationUnchecked(); } } private String getLocationUnchecked() throws Exception { // Get bundle location file. File locFile = new File(m_dir, BUNDLE_LOCATION_FILE); // Read bundle location. FileReader fr = null; BufferedReader br = null; try { fr = new FileReader(locFile); br = new BufferedReader(fr); m_location = br.readLine(); return m_location; } finally { if (br != null) br.close(); if (fr != null) fr.close(); } } public int getPersistentState() throws Exception { if (m_persistentState >= 0) { return m_persistentState; } else if (System.getSecurityManager() != null) { try { return ((Integer) AccessController.doPrivileged( new PrivilegedAction( PrivilegedAction.GET_PERSISTENT_STATE_ACTION, this))).intValue(); } catch (PrivilegedActionException ex) { throw ((PrivilegedActionException) ex).getException(); } } else { return getPersistentStateUnchecked(); } } private int getPersistentStateUnchecked() throws Exception { // Get bundle state file. File stateFile = new File(m_dir, BUNDLE_STATE_FILE); // If the state file doesn't exist, then // assume the bundle was installed. if (!stateFile.exists()) { return Bundle.INSTALLED; } // Read the bundle state. FileReader fr = null; BufferedReader br= null; try { fr = new FileReader(stateFile); br = new BufferedReader(fr); String s = br.readLine(); if (s.equals(ACTIVE_STATE)) { m_persistentState = Bundle.ACTIVE; } else if (s.equals(UNINSTALLED_STATE)) { m_persistentState = Bundle.UNINSTALLED; } else { m_persistentState = Bundle.INSTALLED; } return m_persistentState; } finally { if (br != null) br.close(); if (fr != null) fr.close(); } } public void setPersistentState(int state) throws Exception { if (System.getSecurityManager() != null) { try { AccessController.doPrivileged( new PrivilegedAction( PrivilegedAction.SET_PERSISTENT_STATE_ACTION, this, state)); } catch (PrivilegedActionException ex) { throw ((PrivilegedActionException) ex).getException(); } } else { setPersistentStateUnchecked(state); } } private void setPersistentStateUnchecked(int state) throws Exception { // Get bundle state file. File stateFile = new File(m_dir, BUNDLE_STATE_FILE); // Write the bundle state. FileWriter fw = null; BufferedWriter bw= null; try { fw = new FileWriter(stateFile); bw = new BufferedWriter(fw); String s = null; switch (state) { case Bundle.ACTIVE: s = ACTIVE_STATE; break; case Bundle.UNINSTALLED: s = UNINSTALLED_STATE; break; default: s = INSTALLED_STATE; break; } bw.write(s, 0, s.length()); m_persistentState = state; } catch (IOException ex) { Oscar.error("DefaultBundleArchive: Unable to record state: " + ex);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -