You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2009/07/08 14:40:26 UTC
svn commit: r792115 - in /sling/trunk/contrib/extensions/jcrinstall/osgi/src:
main/java/org/apache/sling/osgi/installer/impl/
main/java/org/apache/sling/osgi/installer/impl/tasks/
test/java/org/apache/sling/osgi/installer/impl/ test/java/org/apache/sli...
Author: bdelacretaz
Date: Wed Jul 8 12:40:25 2009
New Revision: 792115
URL: http://svn.apache.org/viewvc?rev=792115&view=rev
Log:
SLING-1037 - use smaller orderable tasks in jcrinstall's osgi installer
Added:
sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerTaskContext.java (with props)
sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/
sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleInstallRemoveTask.java (with props)
sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleStartTask.java (with props)
sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/ConfigInstallRemoveTask.java (with props)
sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/ConfigurationPid.java
- copied, changed from r791808, sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/ConfigurationPid.java
sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/InstallRemoveTask.java (with props)
sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/SynchronousRefreshPackagesTask.java (with props)
sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/TaskOrder.java (with props)
sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/TaskUtilities.java (with props)
sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/tasks/
sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/tasks/BundleInstallRemoveIgnoreTest.java
- copied, changed from r792082, sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/BundleResourceProcessorIgnoreBundlesTest.java
sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/tasks/ConfigurationPidTest.java
- copied, changed from r792082, sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/ConfigurationPidTest.java
Removed:
sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/BundleResourceProcessor.java
sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/ConfigResourceProcessor.java
sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/ConfigurationPid.java
sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerTaskExecutor.java
sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiResourceProcessorList.java
sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiResourceTask.java
sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/ResourceQueueTask.java
sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/BundleResourceProcessorIgnoreBundlesTest.java
sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/BundleResourceProcessorTest.java
sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/ConfigResourceProcessorTest.java
sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/ConfigurationPidTest.java
sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/OsgiResourceProcessorListTest.java
Modified:
sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/InstallableDataWrapper.java
sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/MissingServiceException.java
sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerImpl.java
sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerTask.java
sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/Storage.java
Modified: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/InstallableDataWrapper.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/InstallableDataWrapper.java?rev=792115&r1=792114&r2=792115&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/InstallableDataWrapper.java (original)
+++ sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/InstallableDataWrapper.java Wed Jul 8 12:40:25 2009
@@ -38,7 +38,7 @@
* the data is actually installed, for example if that data comes
* from a JCR repository that's deactivated due to bundle updates.
*/
-class InstallableDataWrapper implements InstallableData {
+public class InstallableDataWrapper implements InstallableData {
private InstallableData wrapped;
private final File dataFile;
@@ -100,7 +100,7 @@
return wrapped.getDigest();
}
- void cleanup() {
+ public void cleanup() {
if(dataFile != null) {
dataFile.delete();
}
Modified: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/MissingServiceException.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/MissingServiceException.java?rev=792115&r1=792114&r2=792115&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/MissingServiceException.java (original)
+++ sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/MissingServiceException.java Wed Jul 8 12:40:25 2009
@@ -22,8 +22,8 @@
* in which case the task can be retried later
*/
@SuppressWarnings("serial")
-class MissingServiceException extends Exception {
- MissingServiceException(Class<?> serviceClass) {
+public class MissingServiceException extends Exception {
+ public MissingServiceException(Class<?> serviceClass) {
super(serviceClass.getName());
}
}
Modified: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerImpl.java?rev=792115&r1=792114&r2=792115&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerImpl.java (original)
+++ sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerImpl.java Wed Jul 8 12:40:25 2009
@@ -23,13 +23,17 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.TreeSet;
import org.apache.sling.osgi.installer.InstallableData;
import org.apache.sling.osgi.installer.JcrInstallException;
import org.apache.sling.osgi.installer.OsgiController;
import org.apache.sling.osgi.installer.OsgiControllerServices;
-import org.apache.sling.osgi.installer.OsgiResourceProcessor;
import org.apache.sling.osgi.installer.ResourceOverrideRules;
+import org.apache.sling.osgi.installer.impl.tasks.BundleInstallRemoveTask;
+import org.apache.sling.osgi.installer.impl.tasks.BundleStartTask;
+import org.apache.sling.osgi.installer.impl.tasks.ConfigInstallRemoveTask;
+import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.cm.ConfigurationAdmin;
@@ -44,21 +48,24 @@
public class OsgiControllerImpl
implements OsgiController,
OsgiControllerServices,
- OsgiControllerTask.Context {
+ OsgiControllerTaskContext {
private final BundleContext bundleContext;
private final Storage storage;
- private final OsgiResourceProcessorList processors;
private ResourceOverrideRules roRules;
- private final List<OsgiControllerTask> tasks = new LinkedList<OsgiControllerTask>();
- private final OsgiControllerTaskExecutor executor = new OsgiControllerTaskExecutor();
+ private final Set<OsgiControllerTask> tasks = new TreeSet<OsgiControllerTask>();
+ private final Set<OsgiControllerTask> tasksForNextCycle = new TreeSet<OsgiControllerTask>();
+ private final Set<OsgiControllerTask> tasksForThisCycle = new TreeSet<OsgiControllerTask>();
private final ServiceTracker logServiceTracker;
+ private int threadCounter;
+ public static final String BUNDLE_EXTENSION = ".jar";
public static final String STORAGE_FILENAME = "controller.storage";
/** Storage key: digest of an InstallableData */
public static final String KEY_DIGEST = "data.digest";
+ // TODO unused?
private final PackageAdmin packageAdmin;
/** Default value for getLastModified() */
@@ -71,7 +78,6 @@
this.bundleContext = bc;
this.packageAdmin = pa;
this.logServiceTracker = logServiceTracker;
- processors = new OsgiResourceProcessorList(bc, packageAdmin, this);
storage = new Storage(bc.getDataFile(STORAGE_FILENAME));
}
@@ -84,10 +90,6 @@
}
}
- for (OsgiResourceProcessor processor : processors) {
- processor.dispose();
- }
-
if(getLogService() != null) {
getLogService().log(LogService.LOG_WARNING,
OsgiController.class.getName()
@@ -98,15 +100,23 @@
public void scheduleInstallOrUpdate(String uri, InstallableData data) throws IOException, JcrInstallException {
synchronized (tasks) {
- tasks.add(new OsgiResourceTask(uri, data, bundleContext));
+ tasks.add(getTaskForUri(uri, data));
}
}
public void scheduleUninstall(String uri) throws IOException, JcrInstallException {
synchronized (tasks) {
- tasks.add(new OsgiResourceTask(uri, null, bundleContext));
+ tasks.add(getTaskForUri(uri, null));
}
}
+
+ protected OsgiControllerTask getTaskForUri(String uri, InstallableData data) {
+ if(uri.endsWith(BUNDLE_EXTENSION)) {
+ return new BundleInstallRemoveTask(uri, data, bundleContext, this);
+ } else {
+ return new ConfigInstallRemoveTask(uri, data, this);
+ }
+ }
public Set<String> getInstalledUris() {
return storage.getKeys();
@@ -125,51 +135,101 @@
return result;
}
- static String getResourceLocation(String uri) {
+ public static String getResourceLocation(String uri) {
return "jcrinstall://" + uri;
}
/** {@inheritDoc} */
public void executeScheduledOperations() throws Exception {
- // Ready to work?
- if(processors == null) {
- if(getLogService() != null) {
- getLogService().log(LogService.LOG_INFO, "Not activated yet, cannot executeScheduledOperations");
- }
- return;
- }
-
// Anything to do?
if(tasks.isEmpty()) {
return;
}
+ if(getLogService() != null) {
+ getLogService().log(LogService.LOG_INFO, "executeScheduledOperations() starts");
+ }
+
synchronized (tasks) {
- // Add tasks for our processors to execute their own operations,
- // after our own tasks are executed
- for(OsgiResourceProcessor p : processors) {
- tasks.add(new ResourceQueueTask(p));
- }
+ // Add start bundle tasks for all active bundles
+ // so that they are restarted once we're done
+ for(Bundle b : bundleContext.getBundles()) {
+ if(b.getState() == Bundle.ACTIVE) {
+ final OsgiControllerTask t = new BundleStartTask(b.getBundleId());
+ tasks.add(t);
+ if(getLogService() != null) {
+ getLogService().log(LogService.LOG_DEBUG, "Added " + t);
+ }
+ }
+ }
- // Now execute all our tasks in a separate thread
if(getLogService() != null) {
getLogService().log(LogService.LOG_INFO, "Executing " + tasks.size() + " queued tasks");
}
final long start = System.currentTimeMillis();
-
- // execute returns the list of tasks that could not be executed but should be retried later
- // and those have been removed from the tasks list
- final List<OsgiControllerTask> remainingTasks = executor.execute(tasks, this);
- tasks.clear();
- tasks.addAll(remainingTasks);
+
+ // Now execute all our tasks in a separate thread
+ // and re-add tasks scheduled for next cycle
+ executeTasksInSeparateThread();
+ tasks.clear();
+ tasks.addAll(tasksForNextCycle);
+ tasksForNextCycle.clear();
if(getLogService() != null) {
getLogService().log(LogService.LOG_INFO,
- "Done executing queued tasks (" + (System.currentTimeMillis() - start) + " msec)");
+ "executeScheduledOperations() ends (" + (System.currentTimeMillis() - start) + " msec)");
}
}
}
+
+ /** Execute a single task - meant to be called from worker thread */
+ private void executeTask(OsgiControllerTask t) {
+ final long start = System.currentTimeMillis();
+ final LogService log = getLogService();
+ if(log != null) {
+ log.log(LogService.LOG_DEBUG, "Executing task " + t);
+ }
+
+ try {
+ t.execute(this);
+ if(log != null) {
+ final long delta = System.currentTimeMillis() - start;
+ log.log(LogService.LOG_INFO, "Successfully executed " + t + " in " + delta + " msec");
+ }
+ } catch(Throwable th) {
+ if(log != null) {
+ log.log(LogService.LOG_INFO, "Task execution failed (" + th + ") will retry " + t);
+ }
+ tasksForNextCycle.add(t);
+ }
+ }
+
+ void executeTasksInSeparateThread() throws InterruptedException {
+ final Runnable r = new Runnable() {
+ public void run() {
+ while(!tasks.isEmpty()) {
+ final List<OsgiControllerTask> toRemove = new LinkedList<OsgiControllerTask>();
+ for(OsgiControllerTask t : tasks) {
+ toRemove.add(t);
+ executeTask(t);
+ if(!tasksForThisCycle.isEmpty()) {
+ break;
+ }
+ }
+ tasks.removeAll(toRemove);
+ tasks.addAll(tasksForThisCycle);
+ tasksForThisCycle.clear();
+ }
+ }
+ };
+
+ final String threadName = getClass().getSimpleName() + " #" + (++threadCounter);
+ final Thread t = new Thread(r, threadName);
+ t.setDaemon(true);
+ t.start();
+ t.join();
+ }
public void setResourceOverrideRules(ResourceOverrideRules r) {
roRules = r;
@@ -187,10 +247,6 @@
return null;
}
- public OsgiResourceProcessorList getProcessors() {
- return processors;
- }
-
public ResourceOverrideRules getResourceOverrideRules() {
return roRules;
}
@@ -202,4 +258,31 @@
public LogService getLogService() {
return (LogService)logServiceTracker.getService();
}
+
+ public void addTaskToCurrentCycle(OsgiControllerTask t) {
+ if(getLogService() != null) {
+ getLogService().log(LogService.LOG_DEBUG, "adding task to current cycle:" + t);
+ }
+ tasksForThisCycle.add(t);
+ }
+
+ public void addTaskToNextCycle(OsgiControllerTask t) {
+ if(getLogService() != null) {
+ getLogService().log(LogService.LOG_DEBUG, "adding task to next cycle:" + t);
+ }
+ tasksForNextCycle.add(t);
+ }
+
+ public BundleContext getBundleContext() {
+ return bundleContext;
+ }
+
+ public PackageAdmin getPackageAdmin() {
+ return packageAdmin;
+ }
+
+ public OsgiControllerServices getOsgiControllerServices() {
+ return this;
+ }
+
}
\ No newline at end of file
Modified: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerTask.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerTask.java?rev=792115&r1=792114&r2=792115&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerTask.java (original)
+++ sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerTask.java Wed Jul 8 12:40:25 2009
@@ -18,15 +18,28 @@
*/
package org.apache.sling.osgi.installer.impl;
-import org.apache.sling.osgi.installer.ResourceOverrideRules;
-
-/** A Task that can be executed by the OsgiController */
-interface OsgiControllerTask {
- static interface Context {
- ResourceOverrideRules getResourceOverrideRules();
- OsgiResourceProcessorList getProcessors();
- Storage getStorage();
- };
+/** Base class for tasks that can be executed by the OsgiController */
+public abstract class OsgiControllerTask implements Comparable<OsgiControllerTask> {
+ /** Execute this task */
+ public abstract void execute(OsgiControllerTaskContext ctx) throws Exception;
- void execute(Context ctx) throws Exception;
+ /** Tasks are sorted according to this key */
+ public abstract String getSortKey();
+
+ public final int compareTo(OsgiControllerTask o) {
+ return getSortKey().compareTo(o.getSortKey());
+ }
+
+ @Override
+ public final boolean equals(Object o) {
+ if(o instanceof OsgiControllerTask) {
+ return getSortKey().equals(((OsgiControllerTask)o).getSortKey());
+ }
+ return false;
+ }
+
+ @Override
+ public final int hashCode() {
+ return getSortKey().hashCode();
+ }
}
\ No newline at end of file
Added: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerTaskContext.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerTaskContext.java?rev=792115&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerTaskContext.java (added)
+++ sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerTaskContext.java Wed Jul 8 12:40:25 2009
@@ -0,0 +1,41 @@
+/*
+ * 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.osgi.installer.impl;
+
+import org.apache.sling.osgi.installer.OsgiControllerServices;
+import org.apache.sling.osgi.installer.ResourceOverrideRules;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.packageadmin.PackageAdmin;
+
+/** Context for OsgiControllerTask */
+public interface OsgiControllerTaskContext {
+ ResourceOverrideRules getResourceOverrideRules();
+ Storage getStorage();
+ BundleContext getBundleContext();
+ PackageAdmin getPackageAdmin();
+ OsgiControllerServices getOsgiControllerServices();
+
+ /** Schedule a task for execution in the current OsgiController cycle */
+ void addTaskToCurrentCycle(OsgiControllerTask t);
+
+ /** Schedule a task for execution in the next OsgiController cycle,
+ * usually to indicate that a task must be retried
+ */
+ void addTaskToNextCycle(OsgiControllerTask t);
+}
Propchange: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerTaskContext.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/OsgiControllerTaskContext.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Modified: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/Storage.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/Storage.java?rev=792115&r1=792114&r2=792115&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/Storage.java (original)
+++ sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/Storage.java Wed Jul 8 12:40:25 2009
@@ -36,12 +36,15 @@
/** Storage for the OSGi controller, stores Maps for the resources
* managed by the controller.
*/
-class Storage {
+public class Storage {
private final File dataFile;
private final Map<String, Map<String, Object>> data;
private final Logger log = LoggerFactory.getLogger(this.getClass());
+ /** {@link Storage} key for the bundle ID */
+ public static final String KEY_BUNDLE_ID = "bundle.id";
+
/** Create Storage that uses dataFile for persistence, and
* read the current status from that file */
@SuppressWarnings("unchecked")
@@ -79,7 +82,7 @@
}
/** Persist our data to our data file */
- protected void saveToFile() throws IOException {
+ public void saveToFile() throws IOException {
ObjectOutputStream oos = null;
try {
synchronized(data) {
@@ -95,7 +98,7 @@
}
/** True if our data contains give key */
- boolean contains(String key) {
+ public boolean contains(String key) {
return data.containsKey(key);
}
@@ -103,7 +106,7 @@
* If we don't have it yet, and empty map is
* created, but saveData is not called.
*/
- Map<String, Object>getMap(String key) {
+ public Map<String, Object>getMap(String key) {
Map<String, Object> result = data.get(key);
synchronized(data) {
if(result == null) {
@@ -115,7 +118,7 @@
}
/** Remove given key from our storage */
- void remove(String key) {
+ public void remove(String key) {
synchronized(data) {
data.remove(key);
}
Added: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleInstallRemoveTask.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleInstallRemoveTask.java?rev=792115&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleInstallRemoveTask.java (added)
+++ sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleInstallRemoveTask.java Wed Jul 8 12:40:25 2009
@@ -0,0 +1,216 @@
+/*
+ * 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.osgi.installer.impl.tasks;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Map;
+import java.util.jar.Manifest;
+
+import org.apache.sling.osgi.installer.InstallResultCode;
+import org.apache.sling.osgi.installer.InstallableData;
+import org.apache.sling.osgi.installer.OsgiControllerServices;
+import org.apache.sling.osgi.installer.impl.OsgiControllerImpl;
+import org.apache.sling.osgi.installer.impl.OsgiControllerTaskContext;
+import org.apache.sling.osgi.installer.impl.Storage;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+import org.osgi.framework.Version;
+import org.osgi.service.log.LogService;
+
+/** Install/remove task for bundles */
+public class BundleInstallRemoveTask extends InstallRemoveTask {
+
+ private final BundleContext bundleContext;
+ private final OsgiControllerServices ocs;
+
+ public static final String MAVEN_SNAPSHOT_MARKER = "SNAPSHOT";
+
+ public BundleInstallRemoveTask(String uri, InstallableData data, BundleContext ctx, OsgiControllerServices ocs) {
+ super(uri, data);
+ this.bundleContext = ctx;
+ this.ocs = ocs;
+ }
+
+ @Override
+ public String getSortKey() {
+ if(isInstallOrUpdate()) {
+ return TaskOrder.BUNDLE_INSTALL_ORDER + uri;
+ } else {
+ return TaskOrder.BUNDLE_UNINSTALL_ORDER + uri;
+ }
+ }
+
+ @Override
+ protected void doUninstall(OsgiControllerTaskContext tctx, Map<String, Object> attributes) throws Exception {
+ final Long longId = (Long) attributes.get(Storage.KEY_BUNDLE_ID);
+ if (longId == null) {
+ if(ocs.getLogService() != null) {
+ ocs.getLogService().log(LogService.LOG_DEBUG,
+ "Bundle cannot be uninstalled, bundle id not found, ignored: " + uri);
+ }
+ } else {
+ final Bundle b = bundleContext.getBundle(longId);
+ if (b == null) {
+ if(ocs.getLogService() != null) {
+ ocs.getLogService().log(LogService.LOG_DEBUG, "Bundle not found, cannot uninstall (" + longId + ")");
+ }
+ } else {
+ if(ocs.getLogService() != null) {
+ ocs.getLogService().log(LogService.LOG_DEBUG, "Uninstalling Bundle " + b.getLocation());
+ }
+ b.uninstall();
+ tctx.addTaskToCurrentCycle(new SynchronousRefreshPackagesTask());
+ }
+ }
+ }
+
+ @Override
+ protected InstallResultCode doInstallOrUpdate(OsgiControllerTaskContext tctx, Map<String, Object> attributes) throws Exception {
+ int retryCount = 0;
+ if ( attributes.get("RETRY_COUNT") != null ) {
+ retryCount = (Integer)attributes.get("RETRY_COUNT");
+ }
+ retryCount++;
+ attributes.put("RETRY_COUNT", retryCount);
+
+ // Check that we have bundle data and manifest
+ InputStream is = data.adaptTo(InputStream.class);
+ if(is == null) {
+ throw new IOException("InstallableData does not adapt to an InputStream: " + uri);
+ }
+
+ final Manifest m = TaskUtilities.getManifest(data);
+ if(m == null) {
+ throw new IOException("Manifest not found for InstallableData " + uri);
+ }
+
+ // Update if we already have a bundle id, else install
+ Bundle b;
+ boolean updated;
+ try {
+ b = null;
+ updated = false;
+
+ // check whether we know the bundle and it exists
+ final Long longId = (Long) attributes.get(Storage.KEY_BUNDLE_ID);
+ if (longId != null) {
+ b = bundleContext.getBundle(longId);
+ }
+
+ // either we don't know the bundle yet or it does not exist,
+ // so check whether the bundle can be found by its symbolic name
+ if (b == null) {
+ b = TaskUtilities.getMatchingBundle(bundleContext, m);
+ }
+
+ // If the bundle (or one with the same symbolic name) is
+ // already installed, ignore the new one if it's a lower
+ // version
+ if (b != null && retryCount == 1) {
+ final Version installedVersion = new Version((String)(b.getHeaders().get(Constants.BUNDLE_VERSION)));
+ final Version newBundleVersion = new Version(m.getMainAttributes().getValue(Constants.BUNDLE_VERSION));
+ if(ignoreNewBundle(b.getSymbolicName(), uri, installedVersion, newBundleVersion)) {
+ return InstallResultCode.IGNORED;
+ }
+ }
+
+ if (b != null) {
+ // Existing bundle -> stop, update, restart
+ if ( retryCount == 1 ) {
+ if(ocs.getLogService() != null) {
+ ocs.getLogService().log(LogService.LOG_DEBUG, "Calling Bundle.stop() and updating " + uri);
+ }
+ b.stop();
+ b.update(is);
+ b.start();
+ } else {
+ if(ocs.getLogService() != null) {
+ ocs.getLogService().log(LogService.LOG_DEBUG, "Calling Bundle.start " + uri);
+ }
+ b.start();
+ }
+ updated = true;
+ tctx.addTaskToCurrentCycle(new SynchronousRefreshPackagesTask());
+ } else {
+ // New bundle -> install
+ final String fullUri = OsgiControllerImpl.getResourceLocation(uri);
+ int level = data.getBundleStartLevel();
+ if(level > 0) {
+ throw new BundleException("Non-zero start level is not supported anymore (" + level + ")");
+ }
+ b = bundleContext.installBundle(fullUri, is);
+ if(ocs.getLogService() != null) {
+ ocs.getLogService().log(LogService.LOG_DEBUG, "No matching Bundle, installed " + fullUri);
+ }
+ }
+ } finally {
+ // data is never null here
+ try {
+ is.close();
+ } catch (IOException ioe) {
+ }
+ }
+
+ // ensure the bundle id in the attributes, this may be overkill
+ // in simple update situations, but is required for installations
+ // and updates where there are no attributes yet
+ attributes.put(Storage.KEY_BUNDLE_ID, b.getBundleId());
+
+ // start bundle
+ tctx.addTaskToCurrentCycle(new BundleStartTask(b.getBundleId()));
+
+ // TODO - remove retry
+ // Successful - reset retry count
+ retryCount = 0;
+ attributes.put("RETRY_COUNT", retryCount);
+
+ return updated ? InstallResultCode.UPDATED : InstallResultCode.INSTALLED;
+ }
+
+ /** Decide if new bundle musg be ignored, based on the supplied Versions */
+ boolean ignoreNewBundle(String symbolicName, String uri, Version installedVersion, Version newBundleVersion) {
+
+ boolean ignore = false;
+ final int comparison = newBundleVersion.compareTo(installedVersion);
+
+ // Same version but snapshots - do not ignore
+ if(comparison == 0 && installedVersion.toString().contains(MAVEN_SNAPSHOT_MARKER)) {
+ if(ocs.getLogService() != null) {
+ ocs.getLogService().log(LogService.LOG_DEBUG,
+ "Bundle " + symbolicName + " (" + uri + ") "
+ + " has same version (" + installedVersion + ") than installed bundle, but version contains "
+ + MAVEN_SNAPSHOT_MARKER + ", will be updated");
+ }
+
+ } else if (comparison <= 0) {
+ ignore = true;
+ if(ocs.getLogService() != null) {
+ ocs.getLogService().log(LogService.LOG_DEBUG,
+ "Ignoring update of bundle " + symbolicName + " (" + uri
+ + ") as installed version (" + installedVersion + ") is equal or higher than " + newBundleVersion
+ );
+ }
+ }
+
+ return ignore;
+ }
+}
\ No newline at end of file
Propchange: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleInstallRemoveTask.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleInstallRemoveTask.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Added: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleStartTask.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleStartTask.java?rev=792115&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleStartTask.java (added)
+++ sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleStartTask.java Wed Jul 8 12:40:25 2009
@@ -0,0 +1,71 @@
+/*
+ * 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.osgi.installer.impl.tasks;
+
+import java.text.DecimalFormat;
+
+import org.apache.sling.osgi.installer.impl.OsgiControllerTask;
+import org.apache.sling.osgi.installer.impl.OsgiControllerTaskContext;
+import org.osgi.framework.Bundle;
+import org.osgi.service.log.LogService;
+
+/** Task that starts a bundle */
+public class BundleStartTask extends OsgiControllerTask {
+
+ private final long bundleId;
+ private final String sortKey;
+
+ public BundleStartTask(long bundleId) {
+ this.bundleId = bundleId;
+ sortKey = TaskOrder.BUNDLE_START_ORDER + new DecimalFormat("00000").format(bundleId);
+ }
+
+ @Override
+ public String getSortKey() {
+ return sortKey;
+ }
+
+ @Override
+ public String toString() {
+ return getClass().getSimpleName() + " (bundle " + bundleId + ")";
+ }
+
+ public void execute(OsgiControllerTaskContext ctx) throws Exception {
+ final Bundle b = ctx.getBundleContext().getBundle(bundleId);
+ final LogService log = ctx.getOsgiControllerServices().getLogService();
+
+ if(b == null) {
+ if(log != null) {
+ log.log(LogService.LOG_INFO, "Cannot start bundle, id not found:" + bundleId);
+ }
+ return;
+ }
+
+ if(b.getState() == Bundle.ACTIVE) {
+ if(log != null) {
+ log.log(LogService.LOG_DEBUG, "Bundle already started, no action taken:" + bundleId + "/" + b.getSymbolicName());
+ }
+ } else {
+ if(log != null) {
+ log.log(LogService.LOG_INFO, "Starting bundle:" + bundleId + "/" + b.getSymbolicName());
+ }
+ b.start();
+ }
+ }
+}
Propchange: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleStartTask.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleStartTask.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Added: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/ConfigInstallRemoveTask.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/ConfigInstallRemoveTask.java?rev=792115&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/ConfigInstallRemoveTask.java (added)
+++ sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/ConfigInstallRemoveTask.java Wed Jul 8 12:40:25 2009
@@ -0,0 +1,130 @@
+/*
+ * 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.osgi.installer.impl.tasks;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Dictionary;
+import java.util.Map;
+
+import org.apache.sling.osgi.installer.InstallResultCode;
+import org.apache.sling.osgi.installer.InstallableData;
+import org.apache.sling.osgi.installer.JcrInstallException;
+import org.apache.sling.osgi.installer.OsgiControllerServices;
+import org.apache.sling.osgi.installer.impl.DictionaryReader;
+import org.apache.sling.osgi.installer.impl.OsgiControllerTaskContext;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.log.LogService;
+
+/** Install/remove task for configurations */
+public class ConfigInstallRemoveTask extends InstallRemoveTask {
+
+ static final String ALIAS_KEY = "_alias_factory_pid";
+ static final String CONFIG_PATH_KEY = "_jcr_config_path";
+ static final String CONFIG_EXTENSION = ".cfg";
+
+ private final DictionaryReader reader = new DictionaryReader();
+ private final OsgiControllerServices ocs;
+
+ public ConfigInstallRemoveTask(String uri, InstallableData data, OsgiControllerServices ocs) {
+ super(uri, data);
+ this.ocs = ocs;
+ }
+
+ @Override
+ public String getSortKey() {
+ if(isInstallOrUpdate()) {
+ return TaskOrder.CONFIG_INSTALL_ORDER + uri;
+ } else {
+ return TaskOrder.CONFIG_UNINSTALL_ORDER + uri;
+ }
+ }
+
+ @Override
+ protected InstallResultCode doInstallOrUpdate(OsgiControllerTaskContext tctx, Map<String, Object> attributes) throws Exception {
+ // Convert data to a configuration Dictionary
+ Dictionary dict = data.adaptTo(Dictionary.class);
+ if(dict == null) {
+ InputStream is = data.adaptTo(InputStream.class);
+ if (data == null) {
+ throw new IOException("InstallableData does not adapt to an InputStream: " + uri);
+ }
+ try {
+ dict = reader.load(is);
+ } finally {
+ is.close();
+ }
+ }
+
+ if (dict == null) {
+ throw new JcrInstallException("Null Dictionary for uri=" + uri);
+ }
+
+ // Add pseudo-properties
+ dict.put(CONFIG_PATH_KEY, uri);
+
+ // Get pids from node name
+ final ConfigurationPid pid = new ConfigurationPid(uri);
+ if(ocs.getLogService() != null) {
+ ocs.getLogService().log(LogService.LOG_DEBUG,
+ pid + " created for uri " + uri);
+ }
+
+ if(pid.getFactoryPid() != null) {
+ dict.put(ALIAS_KEY, pid.getFactoryPid());
+ }
+
+ // get or create configuration
+ InstallResultCode result = InstallResultCode.UPDATED;
+ Configuration config = TaskUtilities.getConfiguration(pid, false, ocs);
+ if(config == null) {
+ result = InstallResultCode.INSTALLED;
+ config = TaskUtilities.getConfiguration(pid, true, ocs);
+ }
+ if (config.getBundleLocation() != null) {
+ config.setBundleLocation(null);
+ }
+ config.update(dict);
+ if(ocs.getLogService() != null) {
+ ocs.getLogService().log(LogService.LOG_INFO,
+ "Configuration " + config.getPid() + " " + (result == InstallResultCode.UPDATED ? "updated" : "created")
+ );
+ }
+ return result;
+ }
+
+ @Override
+ protected void doUninstall(OsgiControllerTaskContext tctx, Map<String, Object> attributes) throws Exception {
+ final ConfigurationPid pid = new ConfigurationPid(uri);
+ final Configuration cfg = TaskUtilities.getConfiguration(pid, false, ocs);
+ // TODO defer delete if ConfigAdmin not available?
+ if(cfg == null) {
+ if(ocs.getLogService() != null) {
+ ocs.getLogService().log(LogService.LOG_INFO,
+ "Cannot delete config " + uri + ", pid " + pid + " not found, ignored");
+ }
+ } else {
+ if(ocs.getLogService() != null) {
+ ocs.getLogService().log(LogService.LOG_INFO,
+ "Deleting config " + pid + ", uri = " + uri);
+ }
+ cfg.delete();
+ }
+ }
+}
\ No newline at end of file
Propchange: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/ConfigInstallRemoveTask.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/ConfigInstallRemoveTask.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Copied: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/ConfigurationPid.java (from r791808, sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/ConfigurationPid.java)
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/ConfigurationPid.java?p2=sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/ConfigurationPid.java&p1=sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/ConfigurationPid.java&r1=791808&r2=792115&rev=792115&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/ConfigurationPid.java (original)
+++ sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/ConfigurationPid.java Wed Jul 8 12:40:25 2009
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.sling.osgi.installer.impl;
+package org.apache.sling.osgi.installer.impl.tasks;
/** Builds configration PIDs out of filenames, examples:
* o.a.s.foo.bar.cfg -> pid = o.a.s.foo.bar
@@ -35,7 +35,7 @@
}
// cut off extension if it's .cfg
- if(pid.endsWith(ConfigResourceProcessor.CONFIG_EXTENSION)) {
+ if(pid.endsWith(ConfigInstallRemoveTask.CONFIG_EXTENSION)) {
final int lastDot = pid.lastIndexOf('.');
if(lastDot >= 0) {
pid = pid.substring(0, lastDot);
Added: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/InstallRemoveTask.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/InstallRemoveTask.java?rev=792115&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/InstallRemoveTask.java (added)
+++ sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/InstallRemoveTask.java Wed Jul 8 12:40:25 2009
@@ -0,0 +1,136 @@
+/*
+ * 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.osgi.installer.impl.tasks;
+
+import static org.apache.sling.osgi.installer.InstallResultCode.IGNORED;
+
+import java.util.Map;
+
+import org.apache.sling.osgi.installer.InstallResultCode;
+import org.apache.sling.osgi.installer.InstallableData;
+import org.apache.sling.osgi.installer.impl.InstallableDataWrapper;
+import org.apache.sling.osgi.installer.impl.OsgiControllerImpl;
+import org.apache.sling.osgi.installer.impl.OsgiControllerTask;
+import org.apache.sling.osgi.installer.impl.OsgiControllerTaskContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Base class for OsgiControllerTasks that install or
+ * remove OSGi bundles or configs (or deployment packages, etc.)
+ */
+abstract class InstallRemoveTask extends OsgiControllerTask {
+
+ protected final String uri;
+ protected final InstallableData data;
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ protected InstallRemoveTask(String uri, InstallableData data) {
+ this.uri = uri;
+ this.data = data;
+ }
+
+ @Override
+ public String toString() {
+ return
+ getClass().getSimpleName()
+ + ", "
+ + (isInstallOrUpdate() ? "install/update" : "uninstall")
+ + ", "
+ + uri
+ ;
+ }
+
+ /** {@inheritDoc} */
+ public void execute(OsgiControllerTaskContext context) throws Exception {
+ if(isInstallOrUpdate()) {
+ executeInstallOrUpdate(context);
+ } else {
+ executeUninstall(context);
+ }
+
+ // Cleanup InstallableDataWrapper
+ if(data instanceof InstallableDataWrapper) {
+ ((InstallableDataWrapper)data).cleanup();
+ }
+ }
+
+ /** @return true if this is an install/update task, false if it is an uninstall */
+ protected boolean isInstallOrUpdate() {
+ return data != null;
+ }
+
+ protected final void executeInstallOrUpdate(OsgiControllerTaskContext context) throws Exception {
+
+ // If a corresponding higher priority resource is already installed, ignore this one
+ if(context.getResourceOverrideRules() != null) {
+ for(String r : context.getResourceOverrideRules().getHigherPriorityResources(uri)) {
+ if(context.getStorage().contains(r)) {
+ log.info("Resource {} ignored, overridden by {} which has higher priority",
+ uri, r);
+ return;
+ }
+ }
+ }
+
+ // If a corresponding lower priority resource is installed, uninstall it first
+ if(context.getResourceOverrideRules() != null) {
+ for(String r : context.getResourceOverrideRules().getLowerPriorityResources(uri)) {
+ if(context.getStorage().contains(r)) {
+ log.info("Resource {} overrides {}, uninstalling the latter",
+ uri, r);
+ executeUninstall(context);
+ }
+ }
+ }
+
+ // process install
+ final Map<String, Object> map = context.getStorage().getMap(uri);
+ if(doInstallOrUpdate(context, map) != IGNORED) {
+ map.put(OsgiControllerImpl.KEY_DIGEST, data.getDigest());
+ }
+ context.getStorage().saveToFile();
+ }
+
+ protected final void executeUninstall(OsgiControllerTaskContext context) throws Exception {
+ log.info("Execute uninstall " + this);
+ // If a corresponding higher priority resource is installed, ignore this request
+ if(context.getResourceOverrideRules() != null) {
+ for(String r : context.getResourceOverrideRules().getHigherPriorityResources(uri)) {
+ if(context.getStorage().contains(r)) {
+ log.info("Resource {} won't be uninstalled, overridden by {} which has higher priority",
+ uri, r);
+ return;
+ }
+ }
+ }
+
+ doUninstall(context, context.getStorage().getMap(uri));
+
+ context.getStorage().remove(uri);
+ context.getStorage().saveToFile();
+
+ }
+
+ /** Do the actual uninstall */
+ protected abstract void doUninstall(OsgiControllerTaskContext ctx, Map<String, Object> attributes) throws Exception;
+
+ /** Do the actual install or update */
+ protected abstract InstallResultCode doInstallOrUpdate(OsgiControllerTaskContext ctx, Map<String, Object> attributes) throws Exception;
+}
Propchange: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/InstallRemoveTask.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/InstallRemoveTask.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Added: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/SynchronousRefreshPackagesTask.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/SynchronousRefreshPackagesTask.java?rev=792115&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/SynchronousRefreshPackagesTask.java (added)
+++ sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/SynchronousRefreshPackagesTask.java Wed Jul 8 12:40:25 2009
@@ -0,0 +1,104 @@
+/*
+ * 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.osgi.installer.impl.tasks;
+
+import org.apache.sling.osgi.installer.OsgiControllerServices;
+import org.apache.sling.osgi.installer.impl.OsgiControllerTask;
+import org.apache.sling.osgi.installer.impl.OsgiControllerTaskContext;
+import org.osgi.framework.FrameworkEvent;
+import org.osgi.framework.FrameworkListener;
+import org.osgi.service.log.LogService;
+
+/** Task that executes an OSGi "refresh packages" operation, synchronously */
+public class SynchronousRefreshPackagesTask extends OsgiControllerTask implements FrameworkListener {
+
+ /** Max time allowed to refresh packages (TODO configurable??) */
+ public static final int MAX_REFRESH_PACKAGES_WAIT_SECONDS = 30;
+
+ private int packageRefreshEventsCount;
+ private OsgiControllerServices ocs;
+
+ /**
+ * Handles the PACKAGES_REFRESHED framework event which is sent after
+ * the PackageAdmin.refreshPackages has finished its work of refreshing
+ * the packages. When packages have been refreshed all bundles which are
+ * expected to be active (those active before refreshing the packages and
+ * newly installed or updated bundles) are started.
+ */
+ public void frameworkEvent(FrameworkEvent event) {
+ if (event.getType() == FrameworkEvent.PACKAGES_REFRESHED) {
+ if(ocs.getLogService() != null) {
+ ocs.getLogService().log(LogService.LOG_DEBUG,
+ "FrameworkEvent.PACKAGES_REFRESHED");
+ }
+ packageRefreshEventsCount++;
+ }
+ }
+
+ @Override
+ public String getSortKey() {
+ return TaskOrder.REFRESH_PACKAGES_ORDER;
+ }
+
+ @Override
+ public String toString() {
+ return getClass().getSimpleName();
+ }
+
+ public void execute(OsgiControllerTaskContext ctx) throws Exception {
+ final int targetEventCount = packageRefreshEventsCount + 1;
+ final long start = System.currentTimeMillis();
+ final long timeout = System.currentTimeMillis() + MAX_REFRESH_PACKAGES_WAIT_SECONDS * 1000L;
+ ocs = ctx.getOsgiControllerServices();
+
+ // It seems like (at least with Felix 1.0.4) we won't get a FrameworkEvent.PACKAGES_REFRESHED
+ // if one happened very recently and there's nothing to refresh
+ ctx.getPackageAdmin().refreshPackages(null);
+ ctx.getBundleContext().addFrameworkListener(this);
+ try {
+ while(true) {
+ if(System.currentTimeMillis() > timeout) {
+ if(ocs.getLogService() != null) {
+ ocs.getLogService().log(LogService.LOG_WARNING,
+ "No FrameworkEvent.PACKAGES_REFRESHED event received within "
+ + MAX_REFRESH_PACKAGES_WAIT_SECONDS
+ + " seconds after refresh");
+ }
+ break;
+ }
+ if(packageRefreshEventsCount >= targetEventCount) {
+ final long delta = System.currentTimeMillis() - start;
+ if(ocs.getLogService() != null) {
+ ocs.getLogService().log(LogService.LOG_DEBUG,
+ "FrameworkEvent.PACKAGES_REFRESHED received "
+ + delta
+ + " msec after refreshPackages call");
+ }
+ break;
+ }
+ try {
+ Thread.sleep(250L);
+ } catch(InterruptedException ignore) {
+ }
+ }
+ } finally {
+ ctx.getBundleContext().removeFrameworkListener(this);
+ }
+ }
+}
Propchange: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/SynchronousRefreshPackagesTask.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/SynchronousRefreshPackagesTask.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Added: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/TaskOrder.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/TaskOrder.java?rev=792115&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/TaskOrder.java (added)
+++ sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/TaskOrder.java Wed Jul 8 12:40:25 2009
@@ -0,0 +1,29 @@
+/*
+ * 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.osgi.installer.impl.tasks;
+
+/** Define the basic ordering of tasks */
+class TaskOrder {
+ static final String CONFIG_UNINSTALL_ORDER = "10-";
+ static final String CONFIG_INSTALL_ORDER = "20-";
+ static final String BUNDLE_UNINSTALL_ORDER = "30-";
+ static final String BUNDLE_INSTALL_ORDER = "40-";
+ static final String BUNDLE_START_ORDER = "50-";
+ static final String REFRESH_PACKAGES_ORDER = "60-";
+}
Propchange: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/TaskOrder.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/TaskOrder.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Added: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/TaskUtilities.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/TaskUtilities.java?rev=792115&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/TaskUtilities.java (added)
+++ sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/TaskUtilities.java Wed Jul 8 12:40:25 2009
@@ -0,0 +1,120 @@
+package org.apache.sling.osgi.installer.impl.tasks;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.jar.JarInputStream;
+import java.util.jar.Manifest;
+
+import org.apache.sling.osgi.installer.InstallableData;
+import org.apache.sling.osgi.installer.OsgiControllerServices;
+import org.apache.sling.osgi.installer.impl.MissingServiceException;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+
+/** Misc.utilities for classes of this package */
+class TaskUtilities {
+ /**
+ * Returns a bundle with the same symbolic name as the bundle provided in
+ * the installable data. If the installable data has no manifest file or the
+ * manifest file does not have a <code>Bundle-SymbolicName</code> header,
+ * this method returns <code>null</code>. <code>null</code> is also
+ * returned if no bundle with the same symbolic name as provided by the
+ * input stream is currently installed.
+ * <p>
+ * This method gets its own input stream from the installable data object
+ * and closes it after haing read the manifest file.
+ *
+ * @param installableData The installable data providing the bundle jar file
+ * from the input stream.
+ * @return The installed bundle with the same symbolic name as the bundle
+ * provided by the input stream or <code>null</code> if no such
+ * bundle exists or if the input stream does not provide a manifest
+ * with a symbolic name.
+ * @throws IOException If an error occurrs reading from the input stream.
+ */
+ static Bundle getMatchingBundle(BundleContext ctx, Manifest m) throws IOException {
+
+ final String symbolicName = m.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
+ if (symbolicName != null) {
+
+ Bundle[] bundles = ctx.getBundles();
+ for (Bundle bundle : bundles) {
+ if (symbolicName.equals(bundle.getSymbolicName())) {
+ return bundle;
+ }
+ }
+ }
+
+ return null;
+ }
+
+ /** Read the manifest from the InstallableData */
+ static Manifest getManifest(InstallableData data) throws IOException {
+ Manifest result = null;
+ InputStream ins = data.adaptTo(InputStream.class);
+ if (ins == null) {
+ return null;
+ }
+
+ JarInputStream jis = null;
+ try {
+ jis = new JarInputStream(ins);
+ result= jis.getManifest();
+
+ } finally {
+
+ // close the jar stream or the inputstream, if the jar
+ // stream is set, we don't need to close the input stream
+ // since closing the jar stream closes the input stream
+ if (jis != null) {
+ try {
+ jis.close();
+ } catch (IOException ignore) {
+ }
+ } else {
+ // ins is never null here
+ try {
+ ins.close();
+ } catch (IOException ignore) {
+ }
+ }
+ }
+
+ return result;
+ }
+
+ /** Get or create configuration */
+ static Configuration getConfiguration(ConfigurationPid cp, boolean createIfNeeded, OsgiControllerServices ocs)
+ throws IOException, InvalidSyntaxException, MissingServiceException
+ {
+ final ConfigurationAdmin configurationAdmin = ocs.getConfigurationAdmin();
+ if(configurationAdmin == null) {
+ throw new MissingServiceException(ConfigurationAdmin.class);
+ }
+
+ Configuration result = null;
+
+ if (cp.getFactoryPid() == null) {
+ result = configurationAdmin.getConfiguration(cp.getConfigPid(), null);
+ } else {
+ Configuration configs[] = configurationAdmin.listConfigurations(
+ "(|(" + ConfigInstallRemoveTask.ALIAS_KEY
+ + "=" + cp.getFactoryPid() + ")(.alias_factory_pid=" + cp.getFactoryPid()
+ + "))");
+
+ if (configs == null || configs.length == 0) {
+ if(createIfNeeded) {
+ result = configurationAdmin.createFactoryConfiguration(cp.getConfigPid(), null);
+ }
+ } else {
+ result = configs[0];
+ }
+ }
+
+ return result;
+ }
+}
Propchange: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/TaskUtilities.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/contrib/extensions/jcrinstall/osgi/src/main/java/org/apache/sling/osgi/installer/impl/tasks/TaskUtilities.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Copied: sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/tasks/BundleInstallRemoveIgnoreTest.java (from r792082, sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/BundleResourceProcessorIgnoreBundlesTest.java)
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/tasks/BundleInstallRemoveIgnoreTest.java?p2=sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/tasks/BundleInstallRemoveIgnoreTest.java&p1=sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/BundleResourceProcessorIgnoreBundlesTest.java&r1=792082&r2=792115&rev=792115&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/BundleResourceProcessorIgnoreBundlesTest.java (original)
+++ sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/tasks/BundleInstallRemoveIgnoreTest.java Wed Jul 8 12:40:25 2009
@@ -16,77 +16,76 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.sling.osgi.installer.impl;
+package org.apache.sling.osgi.installer.impl.tasks;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
-import org.jmock.Expectations;
-import org.jmock.Mockery;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.FrameworkListener;
+import org.apache.sling.osgi.installer.OsgiControllerServices;
import org.osgi.framework.Version;
-import org.osgi.service.packageadmin.PackageAdmin;
+import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.service.log.LogService;
/** Test ignoring bundle updates based on Versions (SLING-1001) */
-public class BundleResourceProcessorIgnoreBundlesTest {
+public class BundleInstallRemoveIgnoreTest {
private final String symbolicName = "testbundle";
private final String uri = "testuri";
- private BundleResourceProcessor brp;
- private Mockery mockery;
+ private BundleInstallRemoveTask task;
@org.junit.Before public void setup() {
- mockery = new Mockery();
- final BundleContext bc = mockery.mock(BundleContext.class);
- final PackageAdmin pa = mockery.mock(PackageAdmin.class);
-
- mockery.checking(new Expectations() {{
- allowing(bc).addFrameworkListener(with(any(FrameworkListener.class)));
- }});
-
- brp = new BundleResourceProcessor(bc, pa);
+ OsgiControllerServices s = new OsgiControllerServices() {
+ public LogService getLogService() {
+ return null;
+ }
+
+ public ConfigurationAdmin getConfigurationAdmin() {
+ return null;
+ }
+ };
+
+ task = new BundleInstallRemoveTask(null, null, null, s);
}
@org.junit.Test public void testLowerVersion() {
final Version installedVersion = new Version("1.1");
final Version newVersion = new Version("1.0");
assertTrue("Lower version must be ignored",
- brp.ignoreNewBundle(symbolicName, uri, installedVersion, newVersion));
+ task.ignoreNewBundle(symbolicName, uri, installedVersion, newVersion));
}
@org.junit.Test public void testHigherVersion() {
final Version installedVersion = new Version("1.1");
final Version newVersion = new Version("1.2");
assertFalse("Higher version must not be ignored",
- brp.ignoreNewBundle(symbolicName, uri, installedVersion, newVersion));
+ task.ignoreNewBundle(symbolicName, uri, installedVersion, newVersion));
}
@org.junit.Test public void testSameVersion() {
final Version installedVersion = new Version("1.1");
final Version newVersion = new Version("1.1");
assertTrue("Same version must be ignored",
- brp.ignoreNewBundle(symbolicName, uri, installedVersion, newVersion));
+ task.ignoreNewBundle(symbolicName, uri, installedVersion, newVersion));
}
@org.junit.Test public void testSameVersionSnapshot() {
final Version installedVersion = new Version("2.0.5.incubator-SNAPSHOT");
final Version newVersion = new Version("2.0.5.incubator-SNAPSHOT");
assertFalse("Same version snapshot must not be ignored",
- brp.ignoreNewBundle(symbolicName, uri, installedVersion, newVersion));
+ task.ignoreNewBundle(symbolicName, uri, installedVersion, newVersion));
}
@org.junit.Test public void testLowerVersionSnapshot() {
final Version installedVersion = new Version("2.0.5.incubator-SNAPSHOT");
final Version newVersion = new Version("2.0.4.incubator-SNAPSHOT");
assertTrue("Lower version snapshot must be ignored",
- brp.ignoreNewBundle(symbolicName, uri, installedVersion, newVersion));
+ task.ignoreNewBundle(symbolicName, uri, installedVersion, newVersion));
}
@org.junit.Test public void testHigherVersionSnapshot() {
final Version installedVersion = new Version("2.0.5.incubator-SNAPSHOT");
final Version newVersion = new Version("2.0.6.incubator-SNAPSHOT");
assertFalse("Higher version snapshot must not be ignored",
- brp.ignoreNewBundle(symbolicName, uri, installedVersion, newVersion));
+ task.ignoreNewBundle(symbolicName, uri, installedVersion, newVersion));
}
}
Copied: sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/tasks/ConfigurationPidTest.java (from r792082, sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/ConfigurationPidTest.java)
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/tasks/ConfigurationPidTest.java?p2=sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/tasks/ConfigurationPidTest.java&p1=sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/ConfigurationPidTest.java&r1=792082&r2=792115&rev=792115&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/ConfigurationPidTest.java (original)
+++ sling/trunk/contrib/extensions/jcrinstall/osgi/src/test/java/org/apache/sling/osgi/installer/impl/tasks/ConfigurationPidTest.java Wed Jul 8 12:40:25 2009
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.sling.osgi.installer.impl;
+package org.apache.sling.osgi.installer.impl.tasks;
import static org.junit.Assert.assertEquals;