Skip to content

Commit

Permalink
Merge pull request #20 from rwth-acis/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
derjasper authored Oct 16, 2016
2 parents aaeb57f + 20c5d78 commit a5b4a5b
Show file tree
Hide file tree
Showing 43 changed files with 1,128 additions and 222 deletions.
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<property name="ivy.organisation" value="i5" />
<property name="ivy.module" value="las2peer" />
<property name="ivy.revision" value="0.6" />
<property name="ivy.build.number" value="0" />
<property name="ivy.build.number" value="1" />
<property name="ivy.deliver.revision" value="${ivy.revision}" />
<property name="ivy.pom.version" value="${ivy.revision}" />

Expand Down
252 changes: 252 additions & 0 deletions src/main/java/i5/las2peer/api/Context.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
package i5.las2peer.api;

import i5.las2peer.api.exceptions.ArtifactNotFoundException;
import i5.las2peer.api.exceptions.RemoteServiceException;
import i5.las2peer.api.exceptions.ServiceNotAvailableException;
import i5.las2peer.api.exceptions.ServiceNotFoundException;
import i5.las2peer.api.exceptions.StorageException;
import i5.las2peer.execution.L2pThread;
import i5.las2peer.p2p.AgentNotKnownException;
import i5.las2peer.p2p.Node;
import i5.las2peer.persistency.DecodingFailedException;
import i5.las2peer.persistency.Envelope;
import i5.las2peer.security.Agent;
import i5.las2peer.security.AgentLockedException;
import i5.las2peer.security.GroupAgent;
import i5.las2peer.security.L2pSecurityException;
import i5.las2peer.security.ServiceAgent;
import i5.las2peer.tools.CryptoException;
import i5.las2peer.tools.SerializationException;

import java.io.Serializable;
import java.util.List;

/**
* Provides access to the context of the current call.
*
*/
public interface Context {

// old Context methods, to be replaced:

/**
* @return the executing service agent.
*/
public ServiceAgent getServiceAgent();

/**
* @return the current service.
*/
public Service getService();

/**
* @return the calling agent.
*/
public Agent getMainAgent();

/**
* Gets all group agents, which have been unlocked in this context.
*
* @return all (unlocked) group agents of this context
*/
@Deprecated
public GroupAgent[] getGroupAgents();

/**
* Tries to open the given id for this context.
*
* @param groupId
* @return the unlocked GroupAgent of the given id
* @throws AgentNotKnownException
* @throws L2pSecurityException
*/
public GroupAgent requestGroupAgent(long groupId) throws AgentNotKnownException, L2pSecurityException;

/**
* returns an unlocked instance of the requested Agent
*
* @param agentId the requested agent
* @return an unlocked agent instance
* @throws AgentNotKnownException agent not found
* @throws L2pSecurityException agent cannot be unlocked
*/
public Agent requestAgent(long agentId) throws AgentNotKnownException, L2pSecurityException;

/**
* @deprecated Use {@link #fetchEnvelope(String)} instead.
*
* Gets a stored envelope from the p2p network.
*
* @param id
* @return envelope containing the requested data
* @throws ArtifactNotFoundException
* @throws StorageException
*/
@Deprecated
public Envelope getStoredObject(long id) throws ArtifactNotFoundException, StorageException;

/**
* @deprecated Use {@link #fetchEnvelope(String)} instead
*
* Gets a stored envelope from the p2p network. The envelope will be identified by the stored class and
* an arbitrary identifier selected by the using service(s).
*
* @param cls
* @param identifier
* @return envelope containing the requested data
* @throws ArtifactNotFoundException
* @throws StorageException
*/
@Deprecated
public Envelope getStoredObject(Class<?> cls, String identifier) throws ArtifactNotFoundException, StorageException;

/**
* @deprecated Use {@link #fetchEnvelope(String)} instead
*
* Gets a stored envelope from the p2p network. The envelope will be identified by the stored class and
* an arbitrary identifier selected by the using service(s).
*
* @param className
* @param identifier
* @return envelope containing the requested data
* @throws ArtifactNotFoundException
* @throws StorageException
*/
@Deprecated
public Envelope getStoredObject(String className, String identifier) throws ArtifactNotFoundException,
StorageException;

/**
* Gives access to the local node.
*
* @return the local P2P node
*/
public Node getLocalNode();

/**
* Returns agents that are unlocked in this context first. E.g. necessary for opening a received
* {@link i5.las2peer.communication.Message}.
*
* @param id
* @return get the agent of the given id
* @throws AgentNotKnownException
*/
public Agent getAgent(long id) throws AgentNotKnownException;

@Deprecated
public boolean hasAgent(long id);

/**
* Gets the current las2peer context.
*
* @throws IllegalStateException called not in a las2peer execution thread
* @return the current context
*/
public static Context getCurrent() {
return L2pThread.getCurrent();
}

/**
* @deprecated Use {@link i5.las2peer.persistency.Envelope#getContent()}
*
* This method is stub and will be removed soon.
*
* @param envelope the Envelope to unlock
* @throws DecodingFailedException
* @throws L2pSecurityException the MainAgent is not able to open the Envelope
*/
@Deprecated
public void openEnvelope(Envelope envelope) throws DecodingFailedException, L2pSecurityException;

/**
* returns true if the main agent is unlocked and can unlock the given agent
*
* @param agentId an agent id
* @return true if the main agent has access to the given agent, otherwise false
* @throws AgentNotKnownException agent not found
* @throws AgentLockedException main agent is locked
*/
public boolean hasAccess(long agentId) throws AgentNotKnownException, AgentLockedException;

// Envelopes

public void storeEnvelope(Envelope envelope, Agent author) throws StorageException;

public Envelope fetchEnvelope(String identifier) throws StorageException;

public Envelope createEnvelope(String identifier, Serializable content, Agent... reader)
throws IllegalArgumentException, SerializationException, CryptoException;

public Envelope createEnvelope(String identifier, Serializable content, List<Agent> readers)
throws IllegalArgumentException, SerializationException, CryptoException;

public Envelope createEnvelope(Envelope previousVersion, Serializable content, Agent... reader)
throws IllegalArgumentException, SerializationException, CryptoException;

public Envelope createEnvelope(Envelope previousVersion, Serializable content, List<Agent> readers)
throws IllegalArgumentException, SerializationException, CryptoException;

public Envelope createUnencryptedEnvelope(String identifier, Serializable content) throws IllegalArgumentException,
SerializationException, CryptoException;

public Envelope createUnencryptedEnvelope(Envelope previousVersion, Serializable content)
throws IllegalArgumentException, SerializationException, CryptoException;

public void storeEnvelope(Envelope envelope, Agent author, long timeoutMs) throws StorageException;

public void storeEnvelopeAsync(Envelope envelope, Agent author, StorageStoreResultHandler resultHandler,
StorageCollisionHandler collisionHandler, StorageExceptionHandler exceptionHandler);

public Envelope fetchEnvelope(String identifier, long timeoutMs) throws StorageException;

public void fetchEnvelopeAsync(String identifier, StorageEnvelopeHandler envelopeHandler,
StorageExceptionHandler exceptionHandler);

public Envelope createEnvelope(String identifier, Serializable content) throws IllegalArgumentException,
SerializationException, CryptoException;

public Envelope createEnvelope(Envelope previousVersion, Serializable content) throws IllegalArgumentException,
SerializationException, CryptoException;

public void storeEnvelope(Envelope envelope) throws StorageException;

public void storeEnvelope(Envelope envelope, long timeoutMs) throws StorageException;

public void removeEnvelope(String identifier) throws ArtifactNotFoundException, StorageException;

// RMI

/**
* Invokes the method of any other service on behalf of the main agent, thus sending the main agent as calling
* agent.
*
* @param service The service class. A version may be specified (for example package.serviceClass@1.0.0-1 or
* package.serviceClass@1.0). The core tries to find an appropriate version (version 1.0.5 matches 1.0).
* If no version is specified, the newest version is picked.
* @param method The service method.
* @param parameters The parameters list.
* @return The invocation result.
* @throws ServiceNotFoundException If the service is not known to the network.
* @throws ServiceNotAvailableException If the service is temporarily not available.
* @throws RemoteServiceException If the remote service throws an exception.
*/
public Serializable invoke(String service, String method, Serializable... parameters)
throws ServiceNotFoundException, ServiceNotAvailableException, RemoteServiceException;

/**
* Invokes a service method using the agent of this service as calling agent.
*
* @param service The service class. A version may be specified (for example package.serviceClass@1.0.0-1 or
* package.serviceClass@1.0). The core tries to find an appropriate version (version 1.0.5 matches 1.0).
* If no version is specified, the newest version is picked.
* @param method The service method.
* @param parameters The parameters list.
* @return The invocation result.
* @throws ServiceNotFoundException If the service is not known to the network.
* @throws ServiceNotAvailableException If the service is temporarily not available.
* @throws RemoteServiceException If the remote service throws an exception.
*/
public Serializable invokeInterally(String service, String method, Serializable... parameters)
throws ServiceNotFoundException, ServiceNotAvailableException, RemoteServiceException;

}
38 changes: 19 additions & 19 deletions src/main/java/i5/las2peer/api/Service.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package i5.las2peer.api;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import i5.las2peer.execution.L2pServiceException;
import i5.las2peer.execution.L2pThread;
import i5.las2peer.execution.NoSuchServiceMethodException;
Expand All @@ -13,10 +8,15 @@
import i5.las2peer.p2p.Node;
import i5.las2peer.p2p.TimeoutException;
import i5.las2peer.security.Agent;
import i5.las2peer.security.Context;
import i5.las2peer.security.AgentContext;
import i5.las2peer.security.L2pSecurityException;
import i5.las2peer.security.ServiceAgent;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
* Base class for services to be hosted within the las2peer network.
*
Expand All @@ -39,8 +39,8 @@
*
* <p>
* If you want to access the current user agent, the las2peer node or logging from outside your service class, e.g. in
* helper classes or the like, you can make use of the {@link i5.las2peer.security.Context} class, especially of the
* static {@link i5.las2peer.security.Context#getCurrent} method.
* helper classes or the like, you can make use of the {@link i5.las2peer.security.AgentContext} class, especially of
* the static {@link i5.las2peer.security.AgentContext#getCurrent} method.
*
* <h2>Runtime Configuration</h2>
*
Expand Down Expand Up @@ -160,7 +160,7 @@ public Object execute(String method, Object... parameters) throws NoSuchServiceM
* To use the service agent as executing entity, use {@link #invokeInternally(String, String, Serializable...)}
* instead.
*
* Needs an active {@link Context}!
* Needs an active {@link AgentContext}!
*
* @param service The service class. A version may be specified (for example package.serviceClass@1.0.0-1 or
* package.serviceClass@1.0). The core tries to find an appropriate version (version 1.0.5 matches 1.0).
Expand Down Expand Up @@ -188,7 +188,7 @@ public Object invokeServiceMethod(String service, String method, Serializable...
* To use the main agent as executing entity, use {@link #invokeServiceMethod(String, String, Serializable...)}
* instead.
*
* Needs an active {@link Context}!
* Needs an active {@link AgentContext}!
*
* @param service The service class. A version may be specified (for example package.serviceClass@1.0.0-1 or
* package.serviceClass@1.0). The core tries to find an appropriate version (version 1.0.5 matches 1.0).
Expand Down Expand Up @@ -221,8 +221,8 @@ protected Object invokeInternally(String service, String method, Serializable...
* @throws i5.las2peer.execution.NoSuchServiceMethodException
*
*/
public Method searchMethod(String methodName, Object[] params)
throws L2pSecurityException, i5.las2peer.execution.NoSuchServiceMethodException {
public Method searchMethod(String methodName, Object[] params) throws L2pSecurityException,
i5.las2peer.execution.NoSuchServiceMethodException {
Class<?>[] acActualParamTypes = new Class[params.length];
Class<? extends Service> thisClass = this.getClass();

Expand All @@ -245,8 +245,8 @@ public Method searchMethod(String methodName, Object[] params)
for (int i = 0; i < acActualParamTypes.length && bPossible; i++) {
if (!acCheckParamTypes[i].isInstance(params[i])) {
// param[i] is not an instance of the formal parameter type
if (!(acCheckParamTypes[i].isPrimitive()
&& ServiceHelper.getWrapperClass(acCheckParamTypes[i]).isInstance(params[i]))
if (!(acCheckParamTypes[i].isPrimitive() && ServiceHelper.getWrapperClass(
acCheckParamTypes[i]).isInstance(params[i]))
&& !(ServiceHelper.isWrapperClass(acCheckParamTypes[i]) && ServiceHelper
.getUnwrappedClass(acCheckParamTypes[i]).isInstance(params[i]))) {
// and not wrapped or unwrapped either! -> so not more possibilities to match!
Expand Down Expand Up @@ -336,18 +336,18 @@ public final ServiceAgent getAgent() throws AgentNotKnownException {
/**
* Gets the current execution context.
*
* Needs an active {@link Context}!
* Needs an active {@link AgentContext}!
*
* @return the context we're currently running in
*/
public final Context getContext() {
return getL2pThread().getContext();
return getL2pThread();
}

/**
* Gets the current l2p thread.
*
* Needs an active {@link Context}!
* Needs an active {@link AgentContext}!
*
* @return the L2pThread we're currently running in
*/
Expand Down Expand Up @@ -501,7 +501,7 @@ protected void logError(Exception e) {
*/
@Deprecated
protected Node getActiveNode() {
return getL2pThread().getContext().getLocalNode();
return getL2pThread().getCallerContext().getLocalNode();
}

/**
Expand All @@ -513,7 +513,7 @@ protected Node getActiveNode() {
*/
@Deprecated
protected Agent getActiveAgent() {
return getL2pThread().getContext().getMainAgent();
return getL2pThread().getCallerContext().getMainAgent();
}

/**
Expand Down
Loading

0 comments on commit a5b4a5b

Please sign in to comment.