//
// This file is part of the Prose Development Tools for Eclipse package.
//
// The contents of this file are subject to the Mozilla Public License
// Version 1.1 (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.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
// for the specific language governing rights and limitations under the
// License.
//
// The Original Code is Prose Development Tools for Eclipse.
//
// The Initial Developer of the Original Code is Angela Nicoara. Portions
// created by Angela Nicoara are Copyright (C) 2006 Angela Nicoara.
// All Rights Reserved.
//
// Contributor(s):
// $Id: AspectManagerNode.java,v 1.1 2008/11/18 12:23:02 anicoara Exp $
// ==============================================================================
//
package ch.ethz.prose.eclipse.internal.run;
import java.rmi.RemoteException;
import java.util.List;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IType;
import org.eclipse.swt.graphics.Image;
import ch.ethz.prose.eclipse.internal.core.ProseClientRunner;
import ch.ethz.prose.eclipse.internal.core.ProsePlugin;
import ch.ethz.prose.query.AspectSurrogate;
import ch.ethz.prose.tools.RemoteAspectManager;
/**
* @author Angela Nicoara
* @author Johann Gyger
* @version $Id: AspectManagerNode.java,v 1.1 2008/11/18 12:23:02 anicoara Exp $
*/
public class AspectManagerNode extends AbstractRunNode {
/**
* Transaction counter to generate "unique" transaction identifiers.
*/
protected static int transactionCount = 0;
/**
* Prose application run to which the aspect manager belongs.
*/
protected ProseRunNode run;
/**
* Active or testing aspect manager?
*/
protected boolean active;
/**
* Transaction identifier.
*/
protected String transactionId;
/**
* @param run Prose application to which the aspect manager belongs.
* @param active Active or testing aspect manager?
*/
public AspectManagerNode(ProseRunNode run, boolean active) {
this.run = run;
this.active = active;
}
/**
* @return Prose application to which the aspect manager belongs
*/
public ProseRunNode getRun() {
return run;
}
/**
* @return Active or testing aspect manager?
*/
public boolean isActive() {
return active;
}
public String toString() {
return isActive() ? "Active aspect manager" : "Testing aspect manager";
}
/**
* @return Active aspects, or <cod>null</code> if none
*/
public IRunNode[] getChildren() {
try {
List aspects = getAspectManager().allAspects();
AspectNode[] result = new AspectNode[aspects.size()];
for (int i = 0; i < result.length; i++) {
AspectSurrogate aspect = (AspectSurrogate) aspects.get(i);
result[i] = new AspectNode(this, aspect);
}
return result;
} catch (UnreachableException e) {
return null;
} catch (RemoteException e) {
getRun().setUnreachable();
return null;
}
}
/* (non-Javadoc)
* @see ch.ethz.prose.eclipse.internal.run.IRunNode#getParent()
*/
public IRunNode getParent() {
return run;
}
/* (non-Javadoc)
* @see ch.ethz.prose.eclipse.internal.run.IRunNode#getImage()
*/
public Image getImage() {
return ASPECT_MANAGER_IMAGE;
}
/**
* @return Remote aspect manager, or <code>null</code> if the manager couldn't be retrieved.
* @throws UnreachableException
*/
public RemoteAspectManager getAspectManager() throws UnreachableException {
return run.getAspectManagers()[active ? 0 : 1];
}
/**
* @return Transaction identifier, or <code>null</code>
*/
public String getTransactionId() {
return transactionId;
}
/**
* Start a new transaction.
*/
public void startTransaction() {
if (transactionId != null) ProsePlugin.log(new IllegalStateException("Another transaction still active"));
// This is taken from "ch.ethz.prose.tools.MultipleClientModel#startTransaction()".
transactionId = "" + System.currentTimeMillis() + transactionCount--;
}
/**
* Commit a (previously started) transaction.
*
* @throws UnreachableException
*/
public void commitTransaction() throws UnreachableException {
if (transactionId == null) {
ProsePlugin.log(new IllegalStateException("No active transaction"));
return;
}
try {
RemoteAspectManager manager = getAspectManager();
manager.commit(transactionId);
} catch (RemoteException e) {
getRun().setUnreachable();
throw new UnreachableException(e);
} finally {
transactionId = null;
}
}
/**
* Abort a (previously started) transaction.
*
* @throws UnreachableException
*/
public void abortTransaction() throws UnreachableException {
if (transactionId == null) {
ProsePlugin.log(new IllegalStateException("No active transaction"));
return;
}
try {
getAspectManager().abort(transactionId);
} catch (RemoteException e) {
getRun().setUnreachable();
throw new UnreachableException(e);
} finally {
transactionId = null;
}
}
/**
* Insert an aspect.
*
* @param aspect Aspect to insert
* @throws UnreachableException
*/
public void insertAspect(IType aspect) throws UnreachableException {
try {
getAspectManager(); // Check if aspect manager is reachable.
new ProseClientRunner().insertAspect(aspect, getRun().getAddress(), isActive(), transactionId);
ProsePlugin.getDefault().fireAspectInserted(this);
} catch (CoreException e) {
ProsePlugin.log(e.getStatus());
}
}
}