Remove dead code (Event/Store)

git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@703606 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
rfeng 2008-10-10 23:33:46 +00:00
parent 3365b66840
commit fb88610964
34 changed files with 6 additions and 1827 deletions

View file

@ -1,33 +0,0 @@
/*
* 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.tuscany.sca.event;
/**
* The base interface for events that are propagated in the runtime
*
* @version $Rev$ $Date$
*/
public interface Event {
/**
* Returns the source of the event
*/
Object getSource();
}

View file

@ -1,35 +0,0 @@
/*
* 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.tuscany.sca.event;
/**
* Evaluates whether a {@link RuntimeEventListener} is applicable to a given runtime event
*
* @version $Rev$ $Date$
*/
public interface EventFilter {
/**
* Performs the actual evaluation on an event
* @param event Returns true if the event matches implemented criteria
* @return Match result
*/
boolean match(Event event);
}

View file

@ -1,55 +0,0 @@
/*
* 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.tuscany.sca.event;
/**
* Publishes events in the runtime by accepting {@link Event} objects and
* forwarding them to all registered {@link RuntimeEventListener} objects.
*
* @version $Rev$ $Date$
*/
public interface EventPublisher {
/**
* Publish an event to all registered listeners
* @param object The event to publish
*/
void publish(Event object);
/**
* Registers a listener to receive notifications for the context
* @param listener The listener to add
*/
void addListener(RuntimeEventListener listener);
/**
* Registers a listener to receive notifications for the context
* @param filter The filter that will be applied before the lister is called
* @param listener The lister to add
*/
void addListener(EventFilter filter, RuntimeEventListener listener);
/**
* Removes a previously registered listener
* @param listener The listener to remove
*/
void removeListener(RuntimeEventListener listener);
}

View file

@ -1,35 +0,0 @@
/*
* 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.tuscany.sca.event;
import java.util.EventListener;
/**
* Listeners observe events fired in the SCA runtime.
*
* @version $Rev$ $Date$
*/
public interface RuntimeEventListener extends EventListener {
/**
* Accepts and event and acts on it
* @param event The event to act on
*/
void onEvent(Event event);
}

View file

@ -1,37 +0,0 @@
/*
* 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.tuscany.sca.event;
/**
* An event filter that always returns a true condition
*
* @version $Rev$ $Date$
*/
public class TrueFilter implements EventFilter {
/**
* Performs the actual evaluation on an event. Always returns true
* in this case
* @param event The incoming event which is ignored
* @return true
*/
public boolean match(Event event) {
return true;
}
}

View file

@ -1,56 +0,0 @@
/*
* 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.tuscany.sca.store;
/**
* thrown when a record already exists during an insert operation
*
* @version $Rev$ $Date$
*/
public class DuplicateRecordException extends StoreWriteException {
private static final long serialVersionUID = 3116253222569378447L;
/**
* {@inheritDoc}
*/
public DuplicateRecordException() {
super();
}
/**
* {@inheritDoc}
*/
public DuplicateRecordException(String message, Throwable cause) {
super(message, cause);
}
/**
* {@inheritDoc}
*/
public DuplicateRecordException(String message) {
super(message);
}
/**
* {@inheritDoc}
*/
public DuplicateRecordException(Throwable cause) {
super(cause);
}
}

View file

@ -1,46 +0,0 @@
/*
* 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.tuscany.sca.store;
import java.util.UUID;
/**
* Implementations receive callback events during a store recovery operation
*
* @version $Rev$ $Date$
*/
public interface RecoveryListener {
/**
* Signals the start of a recovery
*/
void onBegin();
/**
* Received when a record is recovered
*
* @param id
*/
void onRecord(UUID id);
/**
* Signals the end of recovery
*/
void onEnd();
}

View file

@ -1,96 +0,0 @@
/*
* 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.tuscany.sca.store;
import org.apache.tuscany.sca.event.EventPublisher;
import org.apache.tuscany.sca.runtime.RuntimeComponent;
/**
* Implementations provide a persistent store for runtime data such as conversational state. A persistent store could be
* implemented in a durable fashion using JDBC or a journaling system, or using a non-durable mechanism such as an
* in-memory map.
*
* @version $Rev$ $Date$
*/
public interface Store extends EventPublisher {
/* Used to indicate an the default expiration offset for records for the store */
long DEFAULT_EXPIRATION_OFFSET = -1;
/* Used to indicate an entry should not expire */
long NEVER = -2;
/**
* Adds the given record to the store. Implementations may choose different strategies for writing data such as
* write-through or write-behind.
*
* @param owner the instance owner
* @param id the unique id of the record
* @param object the object representing the data to write
* @param expiration the time in milliseconds when the entry expires
* @throws StoreWriteException if an error occurs during the write operation
*/
void insertRecord(RuntimeComponent owner, String id, Object object, long expiration) throws StoreWriteException;
/**
* Updates a given record in the store, overwriting previous information.
*
* @param owner the instance owner
* @param id the unique id of the record
* @param object the object representing the data to write
* @param expiration the time in milliseconds when the entry expires
* @throws StoreWriteException
*/
void updateRecord(RuntimeComponent owner, String id, Object object, long expiration) throws StoreWriteException;
/**
* Returns the deserialized object in the store corresponding to the given id
*
* @param owner the instance owner
* @param id the unique id of the record
* @return the deserialized object or null if one is not found
* @throws StoreReadException
*/
Object readRecord(RuntimeComponent owner, String id) throws StoreReadException;
/**
* Removes a record from the store
*
* @param owner the instance owner
* @param id the unique id of the record
* @throws StoreWriteException
*/
void removeRecord(RuntimeComponent owner, String id) throws StoreWriteException;
/**
* Removes all records from the store
*
* @throws StoreWriteException
*/
void removeRecords() throws StoreWriteException;
/**
* Initiates a recovery operation, for example during restart after a crash
*
* @param listener the listener to receive recovery callback events
* @throws StoreReadException
*/
void recover(RecoveryListener listener) throws StoreReadException;
}

View file

@ -1,57 +0,0 @@
/*
* 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.tuscany.sca.store;
/**
* Represents a generic exception thrown by a <code>Store</code>
*
* @version $Rev$ $Date$
*/
public class StoreException extends Exception {
private static final long serialVersionUID = -319152147419962709L;
/**
* {@inheritDoc}
*/
public StoreException() {
super();
}
/**
* {@inheritDoc}
*/
public StoreException(String message, Throwable cause) {
super(message, cause);
}
/**
* {@inheritDoc}
*/
public StoreException(String message) {
super(message);
}
/**
* {@inheritDoc}
*/
public StoreException(Throwable cause) {
super(cause);
}
}

View file

@ -1,71 +0,0 @@
/*
* 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.tuscany.sca.store;
import org.apache.tuscany.sca.event.Event;
import org.apache.tuscany.sca.runtime.RuntimeComponent;
/**
* Fired when a store implementation expires a resource
*
* @version $Rev$ $Date$
*/
public class StoreExpirationEvent implements Event {
private Object source;
private RuntimeComponent owner;
private Object instance;
/**
* Constructor.
*
* @param source the source of the event
* @param owner the owner of the expiring object
* @param instance the expiring object
*/
public StoreExpirationEvent(Object source, RuntimeComponent owner, Object instance) {
assert source != null;
assert owner != null;
assert instance != null;
this.source = source;
this.owner = owner;
this.instance = instance;
}
public Object getSource() {
return source;
}
/**
* Returns the owner of the expiring object.
*
* @return the owner of the expiring object.
*/
public RuntimeComponent getOwner() {
return owner;
}
/**
* Returns the expiring object.
*
* @return the expiring object.
*/
public Object getInstance() {
return instance;
}
}

View file

@ -1,71 +0,0 @@
/*
* 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.tuscany.sca.store;
/**
* A generic monitor interface for services to log events
*
* @version $Rev$ $Date$
*/
public interface StoreMonitor {
/**
* Signals the service has started
*
* @param msg
*/
void start(String msg);
/**
* Signals the service has been shutdown
*
* @param msg
*/
void stop(String msg);
/**
* Fired when recovery is started
*/
void beginRecover();
/**
* Fired when recovery is completed
*/
void endRecover();
/**
* Fired when a record is processed during recovery
*
* @param recordId the id of the record being recovered
*/
void recover(Object recordId);
/**
* Signals an error event
*
* @param e the error
*/
void error(Throwable e);
}

View file

@ -1,56 +0,0 @@
/*
* 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.tuscany.sca.store;
/**
* Thrown when an error occurs reading from persistent storage
*
* @version $Rev$ $Date$
*/
public class StoreReadException extends StoreException {
private static final long serialVersionUID = -8624542082121467271L;
/**
* {@inheritDoc}
*/
public StoreReadException() {
super();
}
/**
* {@inheritDoc}
*/
public StoreReadException(String message, Throwable cause) {
super(message, cause);
}
/**
* {@inheritDoc}
*/
public StoreReadException(String message) {
super(message);
}
/**
* {@inheritDoc}
*/
public StoreReadException(Throwable cause) {
super(cause);
}
}

View file

@ -1,56 +0,0 @@
/*
* 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.tuscany.sca.store;
/**
* Thrown when an error occurs writing to persistent storage
*
* @version $Rev$ $Date$
*/
public class StoreWriteException extends StoreException {
private static final long serialVersionUID = 5539070473942048555L;
/**
* {@inheritDoc}
*/
public StoreWriteException() {
super();
}
/**
* {@inheritDoc}
*/
public StoreWriteException(String message, Throwable cause) {
super(message, cause);
}
/**
* {@inheritDoc}
*/
public StoreWriteException(String message) {
super(message);
}
/**
* {@inheritDoc}
*/
public StoreWriteException(Throwable cause) {
super(cause);
}
}

View file

@ -1,63 +0,0 @@
/*
* 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.tuscany.sca.event;
import org.junit.Assert;
import org.junit.Test;
/**
* Test case for TrueFilter
*
* @version $Rev$ $Date$
*/
public class TrueFilterTestCase {
/**
* Test TrueFilter matching an event
*/
@Test
public void testTrueFilterMatch() {
EventFilter filter = new TrueFilter();
Assert.assertTrue(filter.match(new DummyEvent()));
}
/**
* Test TrueFilter matching null
*/
@Test
public void testTrueFilterMatchNull() {
EventFilter filter = new TrueFilter();
Assert.assertTrue(filter.match(null));
}
/**
* Dummy Event implementation for testing
*/
private class DummyEvent implements Event {
/**
* {@inheritDoc}
*/
public Object getSource() {
throw new UnsupportedOperationException();
}
}
}

View file

@ -1,89 +0,0 @@
/*
* 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.tuscany.sca.core.event;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import org.apache.tuscany.sca.event.Event;
import org.apache.tuscany.sca.event.EventFilter;
import org.apache.tuscany.sca.event.EventPublisher;
import org.apache.tuscany.sca.event.RuntimeEventListener;
import org.apache.tuscany.sca.event.TrueFilter;
/**
* Base implementation of an <code>EventPublisher</code>
*
* @version $Rev$ $Date$
*/
public abstract class BaseEventPublisher implements EventPublisher {
protected static final EventFilter TRUE_FILTER = new TrueFilter();
protected Map<EventFilter, List<RuntimeEventListener>> listeners;
public void addListener(RuntimeEventListener listener) {
addListener(TRUE_FILTER, listener);
}
public void removeListener(RuntimeEventListener listener) {
assert listener != null : "Listener cannot be null";
synchronized (getListeners()) {
for (List<RuntimeEventListener> currentList : getListeners().values()) {
for (RuntimeEventListener current : currentList) {
if (current == listener) {
currentList.remove(current);
return;
}
}
}
}
}
public void addListener(EventFilter filter, RuntimeEventListener listener) {
assert listener != null : "Listener cannot be null";
synchronized (getListeners()) {
List<RuntimeEventListener> list = getListeners().get(filter);
if (list == null) {
list = new CopyOnWriteArrayList<RuntimeEventListener>();
listeners.put(filter, list);
}
list.add(listener);
}
}
public void publish(Event event) {
assert event != null : "Event object was null";
for (Map.Entry<EventFilter, List<RuntimeEventListener>> entry : getListeners().entrySet()) {
if (entry.getKey().match(event)) {
for (RuntimeEventListener listener : entry.getValue()) {
listener.onEvent(event);
}
}
}
}
protected Map<EventFilter, List<RuntimeEventListener>> getListeners() {
if (listeners == null) {
listeners = new ConcurrentHashMap<EventFilter, List<RuntimeEventListener>>();
}
return listeners;
}
}

View file

@ -1,54 +0,0 @@
/*
* 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.tuscany.sca.core.event;
import java.net.URI;
import org.apache.tuscany.sca.event.Event;
/**
* Propagated when a component starts
*
* @version $Rev$ $Date$
*/
public class ComponentStart implements Event {
private Object source;
private URI uri;
/**
* Creates a component start event
*
* @param source the source of the event
* @param componentURI the URI of the component being started
*/
public ComponentStart(Object source, URI componentURI) {
this.source = source;
this.uri = componentURI;
}
public URI getComponentURI() {
return uri;
}
public Object getSource() {
return source;
}
}

View file

@ -1,53 +0,0 @@
/*
* 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.tuscany.sca.core.event;
import java.net.URI;
import org.apache.tuscany.sca.event.Event;
/**
* Propagated when a component stops
*
* @version $Rev$ $Date$
*/
public class ComponentStop implements Event {
private Object source;
private URI uri;
/**
* Creates a component stop event
*
* @param source the source of the event
* @param componentUri the composite component associated the component being stopped
*/
public ComponentStop(Object source, URI componentUri) {
this.source = source;
this.uri = componentUri;
}
public URI getComponentURI() {
return uri;
}
public Object getSource() {
return source;
}
}

View file

@ -1,53 +0,0 @@
/*
* 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.tuscany.sca.core.event;
import org.apache.tuscany.sca.event.Event;
/**
* Propagated when a conversation is expired
*
* @version $Rev$ $Date$
*/
public class ConversationEnd implements Event {
private Object source;
private Object id;
/**
* Creates a new event
*
* @param source the source of the event
* @param id the id of the conversation being ended
*/
public ConversationEnd(Object source, Object id) {
this.source = source;
this.id = id;
}
public Object getSource() {
return source;
}
public Object getConversationID() {
return id;
}
}

View file

@ -1,52 +0,0 @@
/*
* 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.tuscany.sca.core.event;
import org.apache.tuscany.sca.event.Event;
/**
* Propagated when a conversation has started
*
* @version $Rev$ $Date$
*/
public class ConversationStart implements Event {
private Object source;
private Object id;
/**
* Creates a new event
*
* @param source the source of the event
* @param id the id of the conversation being started
*/
public ConversationStart(Object source, Object id) {
this.source = source;
this.id = id;
}
public Object getSource() {
return source;
}
public Object getConversationID() {
return id;
}
}

View file

@ -1,53 +0,0 @@
/*
* 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.tuscany.sca.core.event;
import org.apache.tuscany.sca.event.Event;
/**
* Propagated when an HTTP-based session is expired
*
* @version $Rev$ $Date$
*/
public class HttpSessionEnd implements Event {
private Object source;
private Object id;
/**
* Creates a new event
*
* @param source the source of the event
* @param id the id of the HTTP session being ended
*/
public HttpSessionEnd(Object source, Object id) {
this.source = source;
this.id = id;
}
public Object getSource() {
return source;
}
public Object getSessionID() {
return id;
}
}

View file

@ -1,52 +0,0 @@
/*
* 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.tuscany.sca.core.event;
import org.apache.tuscany.sca.event.Event;
/**
* Propagated when an HTTP-based session has started
*
* @version $Rev$ $Date$
*/
public class HttpSessionStart implements Event {
private Object source;
private Object id;
/**
* Creates a new event
*
* @param source the source of the event
* @param id the id of the HTTP session being started
*/
public HttpSessionStart(Object source, Object id) {
this.source = source;
this.id = id;
}
public Object getSource() {
return source;
}
public Object getSessionID() {
return id;
}
}

View file

@ -1,45 +0,0 @@
/*
* 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.tuscany.sca.core.event;
import org.apache.tuscany.sca.event.Event;
/**
* Propagated when a request completes or is ended
*
* @version $Rev$ $Date$
*/
public class RequestEnd implements Event {
private Object source;
/**
* Creates a new event
*
* @param source the source of the event
*/
public RequestEnd(Object source) {
this.source = source;
}
public Object getSource() {
return source;
}
}

View file

@ -1,45 +0,0 @@
/*
* 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.tuscany.sca.core.event;
import org.apache.tuscany.sca.event.Event;
/**
* Propagated when a request is started in the runtime
*
* @version $Rev$ $Date$
*/
public class RequestStart implements Event {
private Object source;
/**
* Creates a new event
*
* @param source the source of the event
*/
public RequestStart(Object source) {
this.source = source;
}
public Object getSource() {
return source;
}
}

View file

@ -22,7 +22,6 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.tuscany.sca.core.context.InstanceWrapper;
import org.apache.tuscany.sca.event.Event;
import org.apache.tuscany.sca.provider.ImplementationProvider;
import org.apache.tuscany.sca.runtime.RuntimeComponent;
@ -88,9 +87,6 @@ public abstract class AbstractScopeContainer<KEY> implements ScopeContainer<KEY>
// do nothing here. the conversational scope container implements this
}
public void onEvent(Event event) {
}
protected boolean isEagerInit() {
ImplementationProvider implementationProvider = ((RuntimeComponent)component).getImplementationProvider();
if (implementationProvider instanceof ScopedImplementationProvider) {

View file

@ -32,7 +32,6 @@ import org.apache.tuscany.sca.core.conversation.ExtendedConversation;
import org.apache.tuscany.sca.core.invocation.ThreadMessageContext;
import org.apache.tuscany.sca.invocation.Message;
import org.apache.tuscany.sca.runtime.RuntimeComponent;
import org.apache.tuscany.sca.store.Store;
/**
* A scope context which manages atomic component instances keyed on ConversationID
@ -44,7 +43,7 @@ public class ConversationalScopeContainer extends AbstractScopeContainer<Object>
private Map<Object, InstanceLifeCycleWrapper> instanceLifecycleCollection =
new ConcurrentHashMap<Object, InstanceLifeCycleWrapper>();
public ConversationalScopeContainer(Store aStore, RuntimeComponent component) {
public ConversationalScopeContainer(RuntimeComponent component) {
super(Scope.CONVERSATION, component);
// Note: aStore is here to preserve the original factory interface. It is not currently used in this

View file

@ -20,21 +20,18 @@
package org.apache.tuscany.sca.core.scope;
import org.apache.tuscany.sca.runtime.RuntimeComponent;
import org.apache.tuscany.sca.store.Store;
/**
* @version $Rev$ $Date$
*/
public class ConversationalScopeContainerFactory implements ScopeContainerFactory {
private Store store;
public ConversationalScopeContainerFactory(Store store) {
public ConversationalScopeContainerFactory() {
super();
this.store = store;
}
public ScopeContainer createScopeContainer(RuntimeComponent component) {
return new ConversationalScopeContainer(store, component);
return new ConversationalScopeContainer(component);
}
public Scope getScope() {

View file

@ -22,7 +22,7 @@ package org.apache.tuscany.sca.core.scope;
/**
* A default scope registry implementation.
*
* @version $Rev: $ $Date: $
* @version $Rev$ $Date$
*/
public class DefaultScopeRegistry extends ScopeRegistryImpl implements ScopeRegistry {
@ -30,7 +30,7 @@ public class DefaultScopeRegistry extends ScopeRegistryImpl implements ScopeRegi
ScopeContainerFactory[] factories =
new ScopeContainerFactory[] {new CompositeScopeContainerFactory(), new StatelessScopeContainerFactory(),
new RequestScopeContainerFactory(),
new ConversationalScopeContainerFactory(null),
new ConversationalScopeContainerFactory(),
// new HttpSessionScopeContainer(monitor)
};
for (ScopeContainerFactory f : factories) {

View file

@ -19,8 +19,6 @@
package org.apache.tuscany.sca.core.scope;
import org.apache.tuscany.sca.core.context.InstanceWrapper;
import org.apache.tuscany.sca.core.event.HttpSessionEnd;
import org.apache.tuscany.sca.event.Event;
import org.apache.tuscany.sca.runtime.RuntimeComponent;
/**
@ -35,16 +33,6 @@ public class HttpSessionScopeContainer extends AbstractScopeContainer<Object> {
super(Scope.SESSION, component);
}
@Override
public void onEvent(Event event) {
checkInit();
if (event instanceof HttpSessionEnd) {
//FIXME key is not used
//Object key = ((HttpSessionEnd)event).getSessionID();
// FIXME: Remove the session id
}
}
@Override
public synchronized void start() {
if (lifecycleState != UNINITIALIZED && lifecycleState != STOPPED) {

View file

@ -22,8 +22,6 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.tuscany.sca.core.context.InstanceWrapper;
import org.apache.tuscany.sca.core.event.RequestEnd;
import org.apache.tuscany.sca.event.Event;
import org.apache.tuscany.sca.runtime.RuntimeComponent;
/**
@ -40,14 +38,6 @@ public class RequestScopeContainer extends AbstractScopeContainer<Thread> {
contexts = new ConcurrentHashMap<Thread, InstanceWrapper>();
}
@Override
public void onEvent(Event event) {
checkInit();
if (event instanceof RequestEnd) {
// shutdownInstances(Thread.currentThread());
}
}
@Override
public synchronized void start() {
if (lifecycleState != UNINITIALIZED && lifecycleState != STOPPED) {

View file

@ -19,7 +19,6 @@
package org.apache.tuscany.sca.core.scope;
import org.apache.tuscany.sca.core.context.InstanceWrapper;
import org.apache.tuscany.sca.event.RuntimeEventListener;
import org.apache.tuscany.sca.runtime.RuntimeComponent;
@ -31,7 +30,7 @@ import org.apache.tuscany.sca.runtime.RuntimeComponent;
* For example, for COMPOSITE scope this could be the URI of the composite component,
* or for HTTP Session scope it might be the HTTP session ID.
*/
public interface ScopeContainer<KEY> extends RuntimeEventListener {
public interface ScopeContainer<KEY> {
/**
* Returns the Scope that this container supports.

View file

@ -1,195 +0,0 @@
/*
* 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.tuscany.sca.core.store;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.apache.tuscany.sca.core.event.BaseEventPublisher;
import org.apache.tuscany.sca.runtime.RuntimeComponent;
import org.apache.tuscany.sca.store.DuplicateRecordException;
import org.apache.tuscany.sca.store.RecoveryListener;
import org.apache.tuscany.sca.store.Store;
import org.apache.tuscany.sca.store.StoreExpirationEvent;
import org.apache.tuscany.sca.store.StoreMonitor;
import org.apache.tuscany.sca.store.StoreWriteException;
import org.osoa.sca.annotations.Destroy;
import org.osoa.sca.annotations.EagerInit;
import org.osoa.sca.annotations.Init;
import org.osoa.sca.annotations.Property;
import org.osoa.sca.annotations.Service;
/**
* Implements a non-durable, non-transactional store using a simple in-memory map
*
* @version $Rev$ $Date$
*/
@Service(Store.class)
@EagerInit
public class MemoryStore extends BaseEventPublisher implements Store {
private Map<RuntimeComponent, Map<String, Record>> store;
// TODO integrate with a core threading scheme
private ScheduledExecutorService scheduler;
private long reaperInterval = 300000;
private StoreMonitor monitor;
private long defaultExpirationOffset = 600000; // 10 minutes
public MemoryStore(StoreMonitor monitor) {
this.monitor = monitor;
this.store = new ConcurrentHashMap<RuntimeComponent, Map<String, Record>>();
this.scheduler = Executors.newSingleThreadScheduledExecutor();
}
/**
* Returns the maximum default expiration offset for records in the store
*
* @return the maximum default expiration offset for records in the store
*/
public long getDefaultExpirationOffset() {
return defaultExpirationOffset;
}
/**
* Sets the maximum default expiration offset for records in the store
*/
@Property
public void setDefaultExpirationOffset(long defaultExpirationOffset) {
this.defaultExpirationOffset = defaultExpirationOffset;
}
/**
* Sets the interval for expired entry scanning to be performed
*/
@Property
public void setReaperInterval(long reaperInterval) {
this.reaperInterval = reaperInterval;
}
public long getReaperInterval() {
return reaperInterval;
}
@Init
public void init() {
scheduler.scheduleWithFixedDelay(new Reaper(), reaperInterval, reaperInterval, TimeUnit.MILLISECONDS);
monitor.start("In-memory store started");
}
@Destroy
public void destroy() {
scheduler.shutdown();
monitor.stop("In-memory store stopped");
}
public void insertRecord(RuntimeComponent owner, String id, Object object, long expiration) throws StoreWriteException {
Map<String, Record> map = store.get(owner);
if (map == null) {
map = new ConcurrentHashMap<String, Record>();
store.put(owner, map);
}
if (map.containsKey(id)) {
throw new DuplicateRecordException("Duplicate record: " + owner.getURI() +" : " + id);
}
map.put(id, new Record(object, expiration));
}
public void updateRecord(RuntimeComponent owner, String id, Object object, long expiration) throws StoreWriteException {
Map<String, Record> map = store.get(owner);
if (map == null) {
throw new StoreWriteException("Record not found: " + owner.getURI() +" : " + id);
}
Record record = map.get(id);
if (record == null) {
throw new StoreWriteException("Record not found: " + owner.getURI() +" : " + id);
}
record.data = object;
}
public Object readRecord(RuntimeComponent owner, String id) {
Map<String, Record> map = store.get(owner);
if (map == null) {
return null;
}
Record record = map.get(id);
if (record != null) {
return record.data;
}
return null;
}
public void removeRecords() {
store.clear();
}
public void removeRecord(RuntimeComponent owner, String id) throws StoreWriteException {
Map<String, Record> map = store.get(owner);
if (map == null) {
throw new StoreWriteException("Owner not found: " + owner.getURI() +" : " + id);
}
if (map.remove(id) == null) {
throw new StoreWriteException("Owner not found: " + owner.getURI() +" : " + id);
}
}
public void recover(RecoveryListener listener) {
throw new UnsupportedOperationException();
}
private class Record {
private Object data;
private long expiration = NEVER;
public Record(Object data, long expiration) {
this.data = data;
this.expiration = expiration;
}
public Object getData() {
return data;
}
public long getExpiration() {
return expiration;
}
}
private class Reaper implements Runnable {
public void run() {
long now = System.currentTimeMillis();
for (Map.Entry<RuntimeComponent, Map<String, Record>> entries : store.entrySet()) {
for (Map.Entry<String, Record> entry : entries.getValue().entrySet()) {
final long expiration = entry.getValue().expiration;
if (expiration != NEVER && now >= expiration) {
RuntimeComponent owner = entries.getKey();
Object instance = entry.getValue().getData();
// notify listeners of the expiration
StoreExpirationEvent event = new StoreExpirationEvent(this, owner, instance);
publish(event);
entries.getValue().remove(entry.getKey());
}
}
}
}
}
}

View file

@ -1,98 +0,0 @@
/*
* 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.tuscany.sca.core.event;
import junit.framework.TestCase;
import org.apache.tuscany.sca.event.Event;
import org.apache.tuscany.sca.event.EventFilter;
import org.apache.tuscany.sca.event.EventPublisher;
import org.apache.tuscany.sca.event.RuntimeEventListener;
import org.apache.tuscany.sca.event.TrueFilter;
import org.easymock.EasyMock;
/**
* @version $Rev$ $Date$
*/
public class BaseEventPublisherTestCase extends TestCase {
EventPublisher publisher;
public void testFireListener() {
Event event = new TestEvent();
RuntimeEventListener listener = EasyMock.createMock(RuntimeEventListener.class);
listener.onEvent(EasyMock.same(event));
EasyMock.expectLastCall();
EasyMock.replay(listener);
publisher.addListener(listener);
publisher.publish(event);
EasyMock.verify(listener);
}
public void testRemoveListener() {
Event event = new TestEvent();
RuntimeEventListener listener = EasyMock.createMock(RuntimeEventListener.class);
EasyMock.replay(listener);
publisher.addListener(listener);
publisher.removeListener(listener);
publisher.publish(event);
EasyMock.verify(listener);
}
public void testFalseFilterListener() {
Event event = new TestEvent();
RuntimeEventListener listener = EasyMock.createMock(RuntimeEventListener.class);
EasyMock.replay(listener);
publisher.addListener(new FalseFilter(), listener);
publisher.publish(event);
EasyMock.verify(listener);
}
public void testTrueFilterListener() {
Event event = new TestEvent();
RuntimeEventListener listener = EasyMock.createMock(RuntimeEventListener.class);
listener.onEvent(EasyMock.same(event));
EasyMock.expectLastCall();
EasyMock.replay(listener);
publisher.addListener(new TrueFilter(), listener);
publisher.publish(event);
EasyMock.verify(listener);
}
@Override
protected void setUp() throws Exception {
publisher = new BaseEventPublisher() {
};
}
private class TestEvent implements Event {
public Object getSource() {
return null;
}
}
private class FalseFilter implements EventFilter {
public boolean match(Event event) {
return false;
}
}
}

View file

@ -1,69 +0,0 @@
/*
* 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.tuscany.sca.core.event;
import java.net.URI;
import junit.framework.TestCase;
/**
* @version $Rev$ $Date$
*/
public class EventTestCase extends TestCase {
private URI uri = URI.create("foo");
public void testCompositeStart() {
ComponentStart event = new ComponentStart(this, uri);
assertEquals(uri, event.getComponentURI());
}
public void testCompositeStop() {
ComponentStop event = new ComponentStop(this, uri);
assertEquals(uri, event.getComponentURI());
}
public void testHttpSessionStart() {
Object id = new Object();
HttpSessionStart event = new HttpSessionStart(this, id);
assertEquals(this, event.getSource());
assertEquals(id, event.getSessionID());
}
public void testHttpSessionEnd() {
Object id = new Object();
HttpSessionEnd event = new HttpSessionEnd(this, id);
assertEquals(this, event.getSource());
assertEquals(id, event.getSessionID());
}
public void testRequestStart() {
RequestStart event = new RequestStart(this);
assertEquals(this, event.getSource());
}
public void testReequestEnd() {
RequestEnd event = new RequestEnd(this);
assertEquals(this, event.getSource());
}
@Override
protected void setUp() throws Exception {
}
}

View file

@ -1,165 +0,0 @@
/*
* 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.tuscany.sca.core.store;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import junit.framework.TestCase;
import org.apache.tuscany.sca.event.RuntimeEventListener;
import org.apache.tuscany.sca.runtime.RuntimeComponent;
import org.apache.tuscany.sca.store.DuplicateRecordException;
import org.apache.tuscany.sca.store.Store;
import org.apache.tuscany.sca.store.StoreExpirationEvent;
import org.apache.tuscany.sca.store.StoreMonitor;
import org.easymock.EasyMock;
import org.easymock.IAnswer;
/**
* @version $Rev$ $Date$
*/
public class MemoryStoreTestCase extends TestCase {
private StoreMonitor monitor;
public void testEviction() throws Exception {
MemoryStore store = new MemoryStore(monitor);
store.setReaperInterval(10);
store.init();
RuntimeComponent component = EasyMock.createNiceMock(RuntimeComponent.class);
EasyMock.replay(component);
String id = UUID.randomUUID().toString();
Object value = new Object();
store.insertRecord(component, id, value, 1);
Thread.sleep(200);
assertNull(store.readRecord(component, id));
store.destroy();
}
public void testNotifyOnEviction() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
RuntimeEventListener listener = EasyMock.createMock(RuntimeEventListener.class);
listener.onEvent(EasyMock.isA(StoreExpirationEvent.class));
EasyMock.expectLastCall().andStubAnswer(new IAnswer<Object>() {
public Object answer() throws Throwable {
latch.countDown();
return null;
}
});
EasyMock.replay(listener);
MemoryStore store = new MemoryStore(monitor);
store.addListener(listener);
store.setReaperInterval(10);
store.init();
RuntimeComponent component = EasyMock.createNiceMock(RuntimeComponent.class);
EasyMock.replay(component);
String id = UUID.randomUUID().toString();
Object value = new Object();
store.insertRecord(component, id, value, 1);
if (!latch.await(1000, TimeUnit.MILLISECONDS)) {
// failed to notify listener
fail();
}
EasyMock.verify(listener);
}
public void testNoEviction() throws Exception {
MemoryStore store = new MemoryStore(monitor);
store.setReaperInterval(10);
store.init();
RuntimeComponent component = EasyMock.createNiceMock(RuntimeComponent.class);
EasyMock.replay(component);
String id = UUID.randomUUID().toString();
Object value = new Object();
store.insertRecord(component, id, value, Store.NEVER);
Thread.sleep(100);
assertNotNull(store.readRecord(component, id));
store.destroy();
}
public void testInsertRecord() throws Exception {
MemoryStore store = new MemoryStore(monitor);
store.setReaperInterval(10);
store.init();
RuntimeComponent component = EasyMock.createNiceMock(RuntimeComponent.class);
EasyMock.replay(component);
String id = UUID.randomUUID().toString();
Object value = new Object();
store.insertRecord(component, id, value, Store.NEVER);
store.destroy();
}
public void testInsertAlreadyExists() throws Exception {
MemoryStore store = new MemoryStore(monitor);
store.setReaperInterval(10);
store.init();
RuntimeComponent component = EasyMock.createMock(RuntimeComponent.class);
EasyMock.expect(component.getURI()).andReturn("component");
EasyMock.replay(component);
String id = UUID.randomUUID().toString();
Object value = new Object();
store.insertRecord(component, id, value, Store.NEVER);
try {
store.insertRecord(component, id, value, Store.NEVER);
fail();
} catch (DuplicateRecordException e) {
//expected
}
store.destroy();
}
public void testUpdateRecord() throws Exception {
MemoryStore store = new MemoryStore(monitor);
store.setReaperInterval(10);
store.init();
RuntimeComponent component = EasyMock.createNiceMock(RuntimeComponent.class);
EasyMock.replay(component);
String id = UUID.randomUUID().toString();
Object value = new Object();
Object newValue = new Object();
store.insertRecord(component, id, value, Store.NEVER);
store.updateRecord(component, id, newValue, 1L);
assertEquals(newValue, store.readRecord(component, id));
store.destroy();
}
public void testDeleteRecord() throws Exception {
MemoryStore store = new MemoryStore(monitor);
store.setReaperInterval(10);
store.init();
RuntimeComponent component = EasyMock.createNiceMock(RuntimeComponent.class);
EasyMock.replay(component);
String id = UUID.randomUUID().toString();
Object value = new Object();
store.insertRecord(component, id, value, Store.NEVER);
store.removeRecord(component, id);
assertNull(store.readRecord(component, id));
store.destroy();
}
@Override
protected void setUp() throws Exception {
monitor = EasyMock.createNiceMock(StoreMonitor.class);
EasyMock.replay(monitor);
}
}