Author: cziegeler Date: Tue Jan 4 09:07:05 2011 New Revision: 1054934 URL: http://svn.apache.org/viewvc?rev=1054934&view=rev Log: SLING-1915 : Status information should be stored outside the bundle data directory
Added: sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/FileUtil.java (with props) Modified: sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/OsgiInstallerImpl.java sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/RegisteredResourceImpl.java sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/DictionaryDigestTest.java sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/MockBundleContext.java sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/RegisteredResourceComparatorTest.java sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/RegisteredResourceTest.java sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/TaskOrderingTest.java sling/trunk/launchpad/base/src/main/resources/sling.properties Added: sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/FileUtil.java URL: http://svn.apache.org/viewvc/sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/FileUtil.java?rev=1054934&view=auto ============================================================================== --- sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/FileUtil.java (added) +++ sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/FileUtil.java Tue Jan 4 09:07:05 2011 @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.sling.installer.core.impl; + +import java.io.File; + +import org.osgi.framework.BundleContext; + +/** + * Utility class for all file handling. + */ +public class FileUtil { + + /** + * The name of the bundle context property defining the location for the + * installer files (value is "sling.installer.dir"). + */ + private static final String CONFIG_DIR = "sling.installer.dir"; + + /** + * The default configuration data directory if no location is configured + * (value is "installer"). + */ + private static final String DEFAULT_DIR = "installer"; + + private final File directory; + + /** + * Create a file util instance and detect the installer directory. + */ + public FileUtil( final BundleContext bundleContext ) { + String location = bundleContext.getProperty(CONFIG_DIR); + + // no configured location, use the config dir in the bundle persistent + // area + if ( location == null ) { + final File locationFile = bundleContext.getDataFile( DEFAULT_DIR ); + if ( locationFile != null ) { + location = locationFile.getAbsolutePath(); + } + } + + // fall back to the current working directory if the platform does + // not support filesystem based data area + if ( location == null ) { + location = System.getProperty( "user.dir" ) + File.separatorChar + DEFAULT_DIR; + } + + // ensure the file is absolute + File locationFile = new File( location ); + if ( !locationFile.isAbsolute() ) { + final File bundleLocationFile = bundleContext.getDataFile( locationFile.getPath() ); + if ( bundleLocationFile != null ) { + locationFile = bundleLocationFile; + } + + // ensure the file object is an absolute file object + locationFile = locationFile.getAbsoluteFile(); + } + + // check the location + if ( !locationFile.isDirectory() ) { + if ( locationFile.exists() ) { + throw new IllegalArgumentException( location + " is not a directory" ); + } + + if ( !locationFile.mkdirs() ) { + throw new IllegalArgumentException( "Cannot create directory " + location ); + } + } + + this.directory = locationFile; + } + + /** + * Return the installer directory. + */ + public File getDirectory() { + return this.directory; + } + + /** + * Return a file with the given name in the installer directory. + * @param fileName The file name + */ + public File getDataFile(final String fileName) { + return new File(this.directory, fileName); + } + + /** Serial number to create unique file names in the data storage. */ + private static long serialNumberCounter = System.currentTimeMillis(); + + private static long getNextSerialNumber() { + synchronized (RegisteredResourceImpl.class) { + return serialNumberCounter++; + } + } + + /** Create a new unique data file. */ + public File createNewDataFile(final String hint) { + final String filename = hint + "-resource-" + getNextSerialNumber() + ".ser"; + return this.getDataFile(filename); + } +} \ No newline at end of file Propchange: sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/FileUtil.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/FileUtil.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Propchange: sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/FileUtil.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/OsgiInstallerImpl.java URL: http://svn.apache.org/viewvc/sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/OsgiInstallerImpl.java?rev=1054934&r1=1054933&r2=1054934&view=diff ============================================================================== --- sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/OsgiInstallerImpl.java (original) +++ sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/OsgiInstallerImpl.java Tue Jan 4 09:07:05 2011 @@ -91,9 +91,12 @@ public class OsgiInstallerImpl private BundleTaskCreator bundleTaskCreator; private ConfigTaskCreator configTaskCreator; + private final FileUtil fileUtil; + /** Constructor */ public OsgiInstallerImpl(final BundleContext ctx) { this.ctx = ctx; + this.fileUtil = new FileUtil(ctx); } /** @@ -129,7 +132,7 @@ public class OsgiInstallerImpl this.configTaskCreator = new ConfigTaskCreator(ctx); this.bundleTaskCreator = new BundleTaskCreator(ctx); setName(getClass().getSimpleName()); - final File f = ctx.getDataFile("RegisteredResourceList.ser"); + final File f = this.fileUtil.getDataFile("RegisteredResourceList.ser"); persistentList = new PersistentResourceList(f); logger.info("Apache Sling OSGi Installer Service started."); } @@ -198,7 +201,7 @@ public class OsgiInstallerImpl createdResources = new ArrayList<RegisteredResource>(); for(final InstallableResource r : resources ) { try { - final RegisteredResource rr = RegisteredResourceImpl.create(ctx, r, scheme); + final RegisteredResource rr = RegisteredResourceImpl.create(ctx, r, scheme, this.fileUtil); createdResources.add(rr); logger.debug("Registering new resource: {}", rr); } catch (final IOException ioe) { Modified: sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/RegisteredResourceImpl.java URL: http://svn.apache.org/viewvc/sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/RegisteredResourceImpl.java?rev=1054934&r1=1054933&r2=1054934&view=diff ============================================================================== --- sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/RegisteredResourceImpl.java (original) +++ sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/RegisteredResourceImpl.java Tue Jan 4 09:07:05 2011 @@ -89,9 +89,6 @@ public class RegisteredResourceImpl /** The current state of this resource. */ private State state = State.INSTALL; - /** Serial number to create unique file names in the data storage. */ - private static long serialNumberCounter = System.currentTimeMillis(); - /** Temporary attributes. */ private transient Map<String, Object> temporaryAttributes; @@ -145,7 +142,8 @@ public class RegisteredResourceImpl */ public static RegisteredResourceImpl create(final BundleContext ctx, final InstallableResource input, - final String scheme) throws IOException { + final String scheme, + final FileUtil fileUtil) throws IOException { // installable resource has an id, a priority and either // an input stream or a dictionary InputStream is = input.getInputStream(); @@ -178,7 +176,8 @@ public class RegisteredResourceImpl resourceType, input.getDigest(), input.getPriority(), - scheme); + scheme, + fileUtil); } /** @@ -194,7 +193,8 @@ public class RegisteredResourceImpl final String type, final String digest, final int priority, - final String scheme) throws IOException { + final String scheme, + final FileUtil fileUtil) throws IOException { this.url = scheme + ':' + id; this.urlScheme = scheme; this.resourceType = type; @@ -203,7 +203,7 @@ public class RegisteredResourceImpl if (resourceType.equals(InstallableResource.TYPE_BUNDLE)) { try { - this.dataFile = getDataFile(ctx); + this.dataFile = fileUtil.createNewDataFile(getType()); copyToLocalStorage(is); setAttributesFromManifest(); final String name = (String)attributes.get(Constants.BUNDLE_SYMBOLICNAME); @@ -258,12 +258,6 @@ public class RegisteredResourceImpl } } - private static long getNextSerialNumber() { - synchronized (RegisteredResourceImpl.class) { - return serialNumberCounter++; - } - } - @Override public String toString() { return "RegisteredResource(url=" + this.getURL() + @@ -272,11 +266,6 @@ public class RegisteredResourceImpl ", digest=" + this.getDigest() + ")"; } - protected File getDataFile(final BundleContext bundleContext) { - final String filename = getType() + "-resource-" + getNextSerialNumber() + ".ser"; - return bundleContext.getDataFile(filename); - } - /** * @see org.apache.sling.installer.core.impl.RegisteredResource#cleanup() */ Modified: sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/DictionaryDigestTest.java URL: http://svn.apache.org/viewvc/sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/DictionaryDigestTest.java?rev=1054934&r1=1054933&r2=1054934&view=diff ============================================================================== --- sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/DictionaryDigestTest.java (original) +++ sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/DictionaryDigestTest.java Tue Jan 4 09:07:05 2011 @@ -26,7 +26,6 @@ import java.util.Dictionary; import java.util.Hashtable; import org.apache.sling.installer.api.InstallableResource; -import org.apache.sling.installer.core.impl.RegisteredResourceImpl; public class DictionaryDigestTest { @@ -38,7 +37,7 @@ public class DictionaryDigestTest { } private RegisteredResourceImpl create(final InstallableResource is) throws IOException { - return RegisteredResourceImpl.create(new MockBundleContext(), is, "test"); + return RegisteredResourceImpl.create(new MockBundleContext(), is, "test", new FileUtil(new MockBundleContext())); } private String testDigestChanged(Dictionary<String, Object> d, Modified: sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/MockBundleContext.java URL: http://svn.apache.org/viewvc/sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/MockBundleContext.java?rev=1054934&r1=1054933&r2=1054934&view=diff ============================================================================== --- sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/MockBundleContext.java (original) +++ sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/MockBundleContext.java Tue Jan 4 09:07:05 2011 @@ -98,12 +98,16 @@ public class MockBundleContext implement } public String getProperty(String key) { - // TODO Auto-generated method stub return null; } public File getDataFile(String filename) { try { + if ( "installer".equals(filename) ) { + final File dir = this.getDataFile("test").getParentFile(); + dir.deleteOnExit(); + return new File(dir, filename); + } final File f = File.createTempFile(filename, ".data"); f.deleteOnExit(); return f; Modified: sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/RegisteredResourceComparatorTest.java URL: http://svn.apache.org/viewvc/sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/RegisteredResourceComparatorTest.java?rev=1054934&r1=1054933&r2=1054934&view=diff ============================================================================== --- sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/RegisteredResourceComparatorTest.java (original) +++ sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/RegisteredResourceComparatorTest.java Tue Jan 4 09:07:05 2011 @@ -30,8 +30,6 @@ import java.util.SortedSet; import java.util.TreeSet; import org.apache.sling.installer.api.InstallableResource; -import org.apache.sling.installer.core.impl.RegisteredResource; -import org.apache.sling.installer.core.impl.RegisteredResourceImpl; import org.junit.Test; public class RegisteredResourceComparatorTest { @@ -56,7 +54,7 @@ public class RegisteredResourceComparato data.put("foo", "bar"); } final InstallableResource r = new InstallableResource(url, null, data, digest, null, priority); - return RegisteredResourceImpl.create(null, r, "test"); + return RegisteredResourceImpl.create(null, r, "test", new FileUtil(new MockBundleContext())); } private void assertOrder(RegisteredResource[] inOrder) { Modified: sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/RegisteredResourceTest.java URL: http://svn.apache.org/viewvc/sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/RegisteredResourceTest.java?rev=1054934&r1=1054933&r2=1054934&view=diff ============================================================================== --- sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/RegisteredResourceTest.java (original) +++ sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/RegisteredResourceTest.java Tue Jan 4 09:07:05 2011 @@ -79,16 +79,17 @@ public class RegisteredResourceTest { @org.junit.Test public void testLocalFileCopy() throws Exception { final File localFile = File.createTempFile("testLocalFileCopy", ".data"); localFile.deleteOnExit(); - final BundleContext bc = new MockBundleContext() { + final BundleContext bc = new MockBundleContext(); + final File f = getTestBundle("testbundle-1.0.jar"); + final InputStream s = new FileInputStream(f); + RegisteredResourceImpl.create(bc, new InstallableResource("test:1.jar", s, null, "somedigest", null, null), "test", new FileUtil(bc) { - public File getDataFile(String filename) { + @Override + public File createNewDataFile(final String hint) { return localFile; } - }; - final File f = getTestBundle("testbundle-1.0.jar"); - final InputStream s = new FileInputStream(f); - RegisteredResourceImpl.create(bc, new InstallableResource("test:1.jar", s, null, "somedigest", null, null), "test"); + }); assertTrue("Local file exists", localFile.exists()); assertEquals("Local file length matches our data", f.length(), localFile.length()); @@ -159,6 +160,6 @@ public class RegisteredResourceTest { } private RegisteredResourceImpl create(final InstallableResource is) throws IOException { - return RegisteredResourceImpl.create(new MockBundleContext(), is, "test"); + return RegisteredResourceImpl.create(new MockBundleContext(), is, "test", new FileUtil(new MockBundleContext())); } } \ No newline at end of file Modified: sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/TaskOrderingTest.java URL: http://svn.apache.org/viewvc/sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/TaskOrderingTest.java?rev=1054934&r1=1054933&r2=1054934&view=diff ============================================================================== --- sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/TaskOrderingTest.java (original) +++ sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/TaskOrderingTest.java Tue Jan 4 09:07:05 2011 @@ -27,9 +27,6 @@ import java.util.Set; import java.util.TreeSet; import org.apache.sling.installer.api.InstallableResource; -import org.apache.sling.installer.core.impl.EntityResourceList; -import org.apache.sling.installer.core.impl.OsgiInstallerTask; -import org.apache.sling.installer.core.impl.RegisteredResourceImpl; import org.apache.sling.installer.core.impl.config.ConfigInstallTask; import org.apache.sling.installer.core.impl.config.ConfigRemoveTask; import org.apache.sling.installer.core.impl.tasks.BundleInstallTask; @@ -55,7 +52,7 @@ public class TaskOrderingTest { final EntityResourceList erl = new EntityResourceList(); erl.addOrUpdate(RegisteredResourceImpl.create(null, new InstallableResource(url, null, new Hashtable<String, Object>(), null, null, null), - "test")); + "test", new FileUtil(new MockBundleContext()))); return erl; } Modified: sling/trunk/launchpad/base/src/main/resources/sling.properties URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/base/src/main/resources/sling.properties?rev=1054934&r1=1054933&r2=1054934&view=diff ============================================================================== --- sling/trunk/launchpad/base/src/main/resources/sling.properties (original) +++ sling/trunk/launchpad/base/src/main/resources/sling.properties Tue Jan 4 09:07:05 2011 @@ -107,6 +107,10 @@ obr.repository.url = http://sling.apache felix.cm.dir = ${sling.home}/config # +# Sling Installer file directory +sling.installer.dir = ${sling.home}/installer + +# # Felix Framework profile directory. This is by default the "felix" directory # below the application home directory. org.osgi.framework.storage = ${sling.home}/felix