Repository: zest-sandbox Updated Branches: refs/heads/develop [created] c5da67d3b refs/heads/gigaspaces_impl [created] 1b2bbf649 refs/heads/master [created] 56c3a5718
Starting on a GigaSpaces implementation. Project: http://git-wip-us.apache.org/repos/asf/zest-sandbox/repo Commit: http://git-wip-us.apache.org/repos/asf/zest-sandbox/commit/1b2bbf64 Tree: http://git-wip-us.apache.org/repos/asf/zest-sandbox/tree/1b2bbf64 Diff: http://git-wip-us.apache.org/repos/asf/zest-sandbox/diff/1b2bbf64 Branch: refs/heads/gigaspaces_impl Commit: 1b2bbf649a7935d297e367967ad541ca0a8cb921 Parents: b74044f Author: Niclas Hedhman <[email protected]> Authored: Sat Dec 18 14:49:06 2010 +0800 Committer: Niclas Hedhman <[email protected]> Committed: Sat Dec 18 14:49:06 2010 +0800 ---------------------------------------------------------------------- extensions/entitystore-gigaspaces/pom.xml | 53 +++++ .../entitystore/gigaspaces/DatabaseExport.java | 34 ++++ .../entitystore/gigaspaces/DatabaseImport.java | 34 ++++ .../gigaspaces/GigaSpacesConfiguration.java | 27 +++ .../GigaSpacesEntityStoreAssembler.java | 46 +++++ .../GigaSpacesEntityStoreService.java | 39 ++++ .../gigaspaces/GigaspacesEntityStoreMixin.java | 204 +++++++++++++++++++ extensions/pom.xml | 1 + 8 files changed, 438 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/zest-sandbox/blob/1b2bbf64/extensions/entitystore-gigaspaces/pom.xml ---------------------------------------------------------------------- diff --git a/extensions/entitystore-gigaspaces/pom.xml b/extensions/entitystore-gigaspaces/pom.xml new file mode 100644 index 0000000..1a3ea8f --- /dev/null +++ b/extensions/entitystore-gigaspaces/pom.xml @@ -0,0 +1,53 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.qi4j</groupId> + <artifactId>qi4j-extensions</artifactId> + <version>1.3-SNAPSHOT</version> + </parent> + + <groupId>org.qi4j.extension</groupId> + <artifactId>entitystore-gigaspaces</artifactId> + <version>1.3-SNAPSHOT</version> + + <dependencies> + <dependency> + <groupId>org.qi4j.core</groupId> + <artifactId>qi4j-core-api</artifactId> + </dependency> + <dependency> + <groupId>org.qi4j.core</groupId> + <artifactId>qi4j-core-spi</artifactId> + </dependency> + <dependency> + <groupId>org.qi4j.core</groupId> + <artifactId>qi4j-core-bootstrap</artifactId> + </dependency> + <dependency> + <groupId>org.qi4j.library</groupId> + <artifactId>qi4j-lib-locking</artifactId> + </dependency> + <dependency> + <groupId>com.gigaspaces</groupId> + <artifactId>gs-runtime</artifactId> + <version>7.1.2</version> + </dependency> + + <!-- For Tests --> + <dependency> + <groupId>org.qi4j.core</groupId> + <artifactId>qi4j-core-testsupport</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.qi4j.core</groupId> + <artifactId>qi4j-core-runtime</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + + +</project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-sandbox/blob/1b2bbf64/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/DatabaseExport.java ---------------------------------------------------------------------- diff --git a/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/DatabaseExport.java b/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/DatabaseExport.java new file mode 100644 index 0000000..bddb682 --- /dev/null +++ b/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/DatabaseExport.java @@ -0,0 +1,34 @@ +/* + * Copyright 2009 Niclas Hedhman. + * + * Licensed 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.qi4j.entitystore.gigaspaces; + +import java.io.IOException; +import java.io.Writer; + +public interface DatabaseExport +{ + /** + * Export data to the writer, with one line per object, in JSON format. + * + * @param out the destination to write the data to. + * @throws java.io.IOException if problems in the underlying stream. + */ + void exportTo( Writer out ) + throws IOException; +} http://git-wip-us.apache.org/repos/asf/zest-sandbox/blob/1b2bbf64/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/DatabaseImport.java ---------------------------------------------------------------------- diff --git a/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/DatabaseImport.java b/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/DatabaseImport.java new file mode 100644 index 0000000..39ced1d --- /dev/null +++ b/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/DatabaseImport.java @@ -0,0 +1,34 @@ +/* + * Copyright 2009 Niclas Hedhman. + * + * Licensed 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.qi4j.entitystore.gigaspaces; + +import java.io.IOException; +import java.io.Reader; + +public interface DatabaseImport +{ + /** + * Import data from the Reader, with one line per object, in JSON format. + * + * @param in The source of the data to be imported. + * @throws java.io.IOException if problems in the underlying stream. + */ + void importFrom( Reader in ) + throws IOException; +} http://git-wip-us.apache.org/repos/asf/zest-sandbox/blob/1b2bbf64/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/GigaSpacesConfiguration.java ---------------------------------------------------------------------- diff --git a/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/GigaSpacesConfiguration.java b/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/GigaSpacesConfiguration.java new file mode 100644 index 0000000..cff7695 --- /dev/null +++ b/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/GigaSpacesConfiguration.java @@ -0,0 +1,27 @@ +/* + * Copyright 2009 Niclas Hedhman. + * + * Licensed 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.qi4j.entitystore.gigaspaces; + +import org.qi4j.api.configuration.ConfigurationComposite; +import org.qi4j.api.property.Property; + +public interface GigaSpacesConfiguration + extends ConfigurationComposite +{ + Property<String> cacheName(); +} http://git-wip-us.apache.org/repos/asf/zest-sandbox/blob/1b2bbf64/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/GigaSpacesEntityStoreAssembler.java ---------------------------------------------------------------------- diff --git a/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/GigaSpacesEntityStoreAssembler.java b/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/GigaSpacesEntityStoreAssembler.java new file mode 100644 index 0000000..007c11c --- /dev/null +++ b/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/GigaSpacesEntityStoreAssembler.java @@ -0,0 +1,46 @@ +/* + * Copyright 2009 Niclas Hedhman. + * + * Licensed 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.qi4j.entitystore.gigaspaces; + +import org.qi4j.api.common.Visibility; +import org.qi4j.bootstrap.Assembler; +import org.qi4j.bootstrap.AssemblyException; +import org.qi4j.bootstrap.ModuleAssembly; +import org.qi4j.entitystore.memory.MemoryEntityStoreService; +import org.qi4j.spi.uuid.UuidIdentityGeneratorService; + +public class GigaSpacesEntityStoreAssembler + implements Assembler +{ + private String configurationModule; + + public GigaSpacesEntityStoreAssembler( String configurationModule ) + { + this.configurationModule = configurationModule; + } + + public void assemble( ModuleAssembly module ) + throws AssemblyException + { + module.addServices( GigaSpacesEntityStoreService.class, UuidIdentityGeneratorService.class ); + ModuleAssembly config = module.layerAssembly().moduleAssembly( configurationModule ); + config.addEntities( GigaSpacesConfiguration.class ).visibleIn( Visibility.layer ); + config.addServices( MemoryEntityStoreService.class ); + } + +} http://git-wip-us.apache.org/repos/asf/zest-sandbox/blob/1b2bbf64/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/GigaSpacesEntityStoreService.java ---------------------------------------------------------------------- diff --git a/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/GigaSpacesEntityStoreService.java b/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/GigaSpacesEntityStoreService.java new file mode 100644 index 0000000..a35f487 --- /dev/null +++ b/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/GigaSpacesEntityStoreService.java @@ -0,0 +1,39 @@ +/* + * Copyright 2009 Niclas Hedhman. + * + * Licensed 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.qi4j.entitystore.gigaspaces; + +import org.qi4j.api.concern.Concerns; +import org.qi4j.api.configuration.Configuration; +import org.qi4j.api.mixin.Mixins; +import org.qi4j.api.service.Activatable; +import org.qi4j.api.service.ServiceComposite; +import org.qi4j.entitystore.map.MapEntityStoreMixin; +import org.qi4j.library.locking.LockingAbstractComposite; +import org.qi4j.spi.entitystore.ConcurrentModificationCheckConcern; +import org.qi4j.spi.entitystore.EntityStateVersions; +import org.qi4j.spi.entitystore.EntityStore; +import org.qi4j.spi.entitystore.StateChangeNotificationConcern; + +@Concerns( { StateChangeNotificationConcern.class, ConcurrentModificationCheckConcern.class } ) +@Mixins( { MapEntityStoreMixin.class, GigaspacesEntityStoreMixin.class } ) +public interface GigaSpacesEntityStoreService + extends EntityStore, EntityStateVersions, DatabaseExport, DatabaseImport, ServiceComposite, Activatable, LockingAbstractComposite, Configuration +{ +} + http://git-wip-us.apache.org/repos/asf/zest-sandbox/blob/1b2bbf64/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/GigaspacesEntityStoreMixin.java ---------------------------------------------------------------------- diff --git a/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/GigaspacesEntityStoreMixin.java b/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/GigaspacesEntityStoreMixin.java new file mode 100644 index 0000000..472817a --- /dev/null +++ b/extensions/entitystore-gigaspaces/src/main/java/org/qi4j/entitystore/gigaspaces/GigaspacesEntityStoreMixin.java @@ -0,0 +1,204 @@ +/* + * Copyright 2009 Niclas Hedhman. + * + * Licensed 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.qi4j.entitystore.gigaspaces; + +import com.tangosol.net.CacheFactory; +import com.tangosol.net.NamedCache; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; +import java.io.StringWriter; +import java.io.UnsupportedEncodingException; +import java.io.Writer; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.locks.ReadWriteLock; +import org.qi4j.api.configuration.Configuration; +import org.qi4j.api.entity.EntityReference; +import org.qi4j.api.injection.scope.This; +import org.qi4j.api.injection.scope.Uses; +import org.qi4j.api.service.Activatable; +import org.qi4j.entitystore.map.MapEntityStore; +import org.qi4j.spi.entity.EntityType; +import org.qi4j.spi.entitystore.EntityNotFoundException; +import org.qi4j.spi.entitystore.EntityStoreException; +import org.qi4j.spi.service.ServiceDescriptor; + +public class GigaspacesEntityStoreMixin + implements Activatable, MapEntityStore, DatabaseExport, DatabaseImport +{ + @This + private ReadWriteLock lock; + + @This + private Configuration<GigaSpacesConfiguration> config; + + @Uses + private ServiceDescriptor descriptor; + + private NamedCache cache; + + // Activatable implementation + public void activate() + throws Exception + { + String cacheName = config.configuration().cacheName().get(); + cache = CacheFactory.getCache( cacheName ); + } + + public void passivate() + throws Exception + { + cache.destroy(); + } + + public Reader get( EntityReference entityReference ) + throws EntityStoreException + { + byte[] data = (byte[]) cache.get( entityReference.identity() ); + + if( data == null ) + { + throw new EntityNotFoundException( entityReference ); + } + try + { + return new StringReader( new String( data, "UTF-8" ) ); + } + catch( UnsupportedEncodingException e ) + { + // Can not happen. + throw new InternalError(); + } + } + + public void applyChanges( MapChanges changes ) + throws IOException + { + try + { + changes.visitMap( new MapChanger() + { + public Writer newEntity( final EntityReference ref, EntityType entityType ) + throws IOException + { + return new StringWriter( 1000 ) + { + @Override + public void close() + throws IOException + { + super.close(); + + byte[] stateArray = toString().getBytes( "UTF-8" ); + cache.put( ref.identity(), stateArray ); + } + }; + } + + public Writer updateEntity( final EntityReference ref, EntityType entityType ) + throws IOException + { + return new StringWriter( 1000 ) + { + @Override + public void close() + throws IOException + { + super.close(); + byte[] stateArray = toString().getBytes( "UTF-8" ); + cache.put( ref.identity(), stateArray ); + } + }; + } + + public void removeEntity( EntityReference ref, EntityType entityType ) + throws EntityNotFoundException + { + cache.remove( ref.identity() ); + } + } ); + } + catch( Exception e ) + { + if( e instanceof IOException ) + { + throw (IOException) e; + } + else if( e instanceof EntityStoreException ) + { + throw (EntityStoreException) e; + } + else + { + IOException exception = new IOException(); + exception.initCause( e ); + throw exception; + } + } + } + + public void visitMap( MapEntityStoreVisitor visitor ) + { + Iterator<Map.Entry<String, byte[]>> list = cache.entrySet().iterator(); + while( list.hasNext() ) + { + Map.Entry<String, byte[]> entry = list.next(); + String id = entry.getKey(); + byte[] data = entry.getValue(); + try + { + visitor.visitEntity( new StringReader( new String( data, "UTF-8" ) ) ); + } + catch( UnsupportedEncodingException e ) + { + // Can not happen! + } + } + } + + public void exportTo( Writer out ) + throws IOException + { + Iterator<Map.Entry<String, byte[]>> list = cache.entrySet().iterator(); + while( list.hasNext() ) + { + Map.Entry<String, byte[]> entry = list.next(); + byte[] data = entry.getValue(); + String value = new String( data, "UTF-8" ); + out.write( value ); + out.write( '\n' ); + } + } + + public void importFrom( Reader in ) + throws IOException + { + BufferedReader reader = new BufferedReader( in ); + String object; + while( ( object = reader.readLine() ) != null ) + { + String id = object.substring( "{\"identity\":\"".length() ); + id = id.substring( 0, id.indexOf( '"' ) ); + byte[] stateArray = object.getBytes( "UTF-8" ); + cache.put( id, stateArray ); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-sandbox/blob/1b2bbf64/extensions/pom.xml ---------------------------------------------------------------------- diff --git a/extensions/pom.xml b/extensions/pom.xml index c808f39..852552d 100644 --- a/extensions/pom.xml +++ b/extensions/pom.xml @@ -13,6 +13,7 @@ <modules> <!--module>entitystore-coherence</module--> + <module>entitystore-gigaspaces</module> <module>entitystore-jndi</module> <module>entitystore-javaspaces</module> <module>entitystore-jgroups</module>
