Skip to content
Permalink
Browse files
FELIX-4409/4410:
- improve logging in case a bundle update fails by including the BSN
  and version(s);
- made the update commands a little more resilient againts unchecked
  exceptions to ensure that an (un)installation of a DP always yields
  a DeploymentException in case of failures;
- added JUnit test to test the behaviour of DA when an invalid DP is
  given (e.g., containing two bundles with the same BSN).



git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1563873 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Jan Willem Janssen committed Feb 3, 2014
1 parent e88fbd8 commit a55c41beb8f26397554741d62e4cd0cfb935f6ab
Show file tree
Hide file tree
Showing 18 changed files with 474 additions and 363 deletions.
@@ -0,0 +1,46 @@
/*
* 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.felix.deploymentadmin.spi;

/**
* Base implementation for commit/rollback actions with proper error handling.
*
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
*/
abstract class AbstractAction implements Runnable {
/**
* Runs this action by calling {@link #doRun()} and in case of failure,
* {@link #onFailure(Exception)}.
*/
public final void run() {
try {
doRun();
}
catch (Exception e) {
onFailure(e);
}
}

protected abstract void doRun() throws Exception;

protected void onFailure(Exception e) {
// Nop
}
}
@@ -18,6 +18,8 @@
*/
package org.apache.felix.deploymentadmin.spi;

import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
@@ -27,51 +29,89 @@
import org.osgi.service.deploymentadmin.DeploymentException;

/**
* Commands describe a group of tasks to be executed within the execution a deployment session.
* A command that has already executed can be rolled back and a command that is currently in progress
* can be signaled to stop it's activities by canceling it.
* Commands describe a group of tasks to be executed within the execution a
* deployment session. A command that has already executed can be rolled back
* and a command that is currently in progress can be signaled to stop it's
* activities by canceling it.
*/
public abstract class Command {

private final List m_rollback = new ArrayList();
private final List m_commit = new ArrayList();

private volatile boolean m_cancelled;

/**
* Executes the command, the specified <code>DeploymentSession</code> can be used to obtain various
* information about the deployment session which the command is part of.
*
* Executes the command, the specified <code>DeploymentSession</code> can be
* used to obtain various information about the deployment session which the
* command is part of.
*
* @param session The deployment session this command is part of.
* @throws DeploymentException Thrown if the command could not successfully execute.
* @throws DeploymentException Thrown if the command could not successfully
* execute.
*/
public abstract void execute(DeploymentSessionImpl session) throws DeploymentException;
public final void execute(DeploymentSessionImpl session) throws DeploymentException {
try {
doExecute(session);
}
catch (Exception e) {
if (e instanceof DeploymentException) {
throw (DeploymentException) e;
}
throw new DeploymentException(DeploymentException.CODE_OTHER_ERROR, "Command failed!", e);
}
}

/**
* Rolls back all actions that were added through the <code>addRollback(Runnable r)</code> method (in reverse
* adding order). It is not guaranteed that the state of everything related to the command will be as if the
* command was never executed, a best effort should be made though.
* Executes the command, the specified <code>DeploymentSession</code> can be
* used to obtain various information about the deployment session which the
* command is part of.
*
* @param session the deployment session to rollback, should be used for logging purposes.
* @param session The deployment session this command is part of.
* @throws Exception in case of this command could not terminate
* successfully.
*/
protected abstract void doExecute(DeploymentSessionImpl session) throws Exception;

/**
* Rolls back all actions that were added through the
* <code>addRollback(Runnable r)</code> method (in reverse adding order). It
* is not guaranteed that the state of everything related to the command
* will be as if the command was never executed, a best effort should be
* made though.
*
* @param session the deployment session to rollback, should be used for
* logging purposes.
*/
protected void rollback(DeploymentSessionImpl session) {
for (ListIterator i = m_rollback.listIterator(m_rollback.size()); i.hasPrevious();) {
Runnable runnable = (Runnable) i.previous();
runnable.run();
try {
for (ListIterator i = m_rollback.listIterator(m_rollback.size()); i.hasPrevious();) {
Runnable runnable = (Runnable) i.previous();
runnable.run();
}
}
finally {
cleanUp();
}
cleanUp();
}

/**
* Commits all changes the command may have defined when it was executed by calling the <code>execute()</code> method.
* Commits all changes the command may have defined when it was executed by
* calling the <code>execute()</code> method.
*
* @param session the deployment session to commit, should be used for logging purposes.
* @param session the deployment session to commit, should be used for
* logging purposes.
*/
protected void commit(DeploymentSessionImpl session) {
for (ListIterator i = m_commit.listIterator(); i.hasNext();) {
Runnable runnable = (Runnable) i.next();
runnable.run();
protected final void commit(DeploymentSessionImpl session) {
try {
for (ListIterator i = m_commit.listIterator(); i.hasNext();) {
Runnable runnable = (Runnable) i.next();
runnable.run();
}
}
finally {
cleanUp();
}
cleanUp();
}

private void cleanUp() {
@@ -81,9 +121,11 @@ private void cleanUp() {
}

/**
* Determines if the command was canceled. This method should be used regularly by implementing classes to determine if
* their command was canceled, if so they should return as soon as possible from their operations.
*
* Determines if the command was canceled. This method should be used
* regularly by implementing classes to determine if their command was
* canceled, if so they should return as soon as possible from their
* operations.
*
* @return true if the command was canceled, false otherwise.
*/
protected boolean isCancelled() {
@@ -92,38 +134,53 @@ protected boolean isCancelled() {

/**
* Adds an action to be executed in case of a roll back.
*
*
* @param runnable The runnable to be executed in case of a roll back.
*/
protected void addRollback(Runnable runnable) {
protected void addRollback(AbstractAction runnable) {
m_rollback.add(runnable);
}

/**
* Adds an action to be executed in case of a commit
*
*
* @param runnable The runnable to be executes in case of a commit.
*/
protected void addCommit(Runnable runnable) {
protected void addCommit(AbstractAction runnable) {
m_commit.add(runnable);
}

/**
* Sets the command to being cancelled, this does not have an immediate effect. Commands that are executing should
* check regularly if they were cancelled and if so they should make an effort to stop their operations as soon as possible
* followed by throwing an <code>DeploymentException.CODE_CANCELLED</code> exception.
* Sets the command to being cancelled, this does not have an immediate
* effect. Commands that are executing should check regularly if they were
* cancelled and if so they should make an effort to stop their operations
* as soon as possible followed by throwing an
* <code>DeploymentException.CODE_CANCELLED</code> exception.
*/
public void cancel() {
m_cancelled = true;
}

/**
* Determines whether a given bundle is actually a fragment bundle.
*
* @param bundle the bundle to test, cannot be <code>null</code>.
* @return <code>true</code> if the given bundle is actually a fragment bundle, <code>false</code> otherwise.
* @return <code>true</code> if the given bundle is actually a fragment
* bundle, <code>false</code> otherwise.
*/
static final boolean isFragmentBundle(Bundle bundle) {
Object fragmentHost = bundle.getHeaders().get(Constants.FRAGMENT_HOST);
return fragmentHost != null;
}

static final void closeSilently(Closeable resource) {
if (resource != null) {
try {
resource.close();
}
catch (IOException e) {
// Not much we can do...
}
}
}
}
@@ -29,15 +29,16 @@
import org.osgi.service.log.LogService;

/**
* Command that commits all the resource processors that were added to the command.
* Command that commits all the resource processors that were added to the
* command.
*/
public class CommitResourceCommand extends Command {

private final List m_processors = new ArrayList();

public void execute(DeploymentSessionImpl session) throws DeploymentException {
protected void doExecute(DeploymentSessionImpl session) throws Exception {
for (ListIterator i = m_processors.listIterator(m_processors.size()); i.hasPrevious();) {
ResourceProcessor processor = (ResourceProcessor) i.previous();
ResourceProcessor processor = (ResourceProcessor) i.previous();
try {
processor.prepare();
}
@@ -76,17 +77,20 @@ protected void rollback(DeploymentSessionImpl session) {
catch (Exception e) {
// We cannot throw an exception, see OSGi spec.
session.getLog().log(LogService.LOG_ERROR, "Rollback of resource processor '" + processor + "' failed", e);
} finally {
}
finally {
i.remove();
}
}
}

/**
* Add a resource processor, all resource processors that are added will be committed when the command is executed.
*
* Add a resource processor, all resource processors that are added will be
* committed when the command is executed.
*
* @param processor The resource processor to add.
* @return true if the resource processor was added, false if it was already added.
* @return true if the resource processor was added, false if it was already
* added.
*/
public boolean addResourceProcessor(ResourceProcessor processor) {
for (Iterator i = m_processors.iterator(); i.hasNext();) {

0 comments on commit a55c41b

Please sign in to comment.