Author: matthijsh Date: Thu Dec 22 14:54:03 2011 New Revision: 1874 Log: [SANDBOX] Added cassandra-example
Added: sandbox/matthijsh/cassandra-example/ sandbox/matthijsh/cassandra-example/pom.xml sandbox/matthijsh/cassandra-example/profile-service/ sandbox/matthijsh/cassandra-example/profile-service/pom.xml sandbox/matthijsh/cassandra-example/profile-service/src/ sandbox/matthijsh/cassandra-example/profile-service/src/main/ sandbox/matthijsh/cassandra-example/profile-service/src/main/java/ sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/ sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/ sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/ sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/ sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/ sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/ sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/Profile.java sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/ProfileService.java sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/ProfileStore.java sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/ProfileStoreException.java sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/ResponseException.java sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/bean/ sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/bean/ProfileBean.java sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/osgi/ sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/osgi/Activator.java sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/service/ sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/service/ProfileColumnFamilyProviderImpl.java sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/service/ProfileImpl.java sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/service/ProfileServiceImpl.java sandbox/matthijsh/cassandra-example/profile-service/src/main/resources/ Added: sandbox/matthijsh/cassandra-example/pom.xml ============================================================================== --- (empty file) +++ sandbox/matthijsh/cassandra-example/pom.xml Thu Dec 22 14:54:03 2011 @@ -0,0 +1,121 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright (c) 2010, 2011 The Amdatu Foundation + + 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.verning permissions and limitations + under the License. +--> +<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/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.amdatu.cassandra</groupId> + <artifactId>org.amdatu.cassandra</artifactId> + <version>0.2.2-SNAPSHOT</version> + </parent> + <groupId>org.amdatu.cassandra</groupId> + <artifactId>org.amdatu.cassandra.example</artifactId> + <version>0.2.2-SNAPSHOT</version> + <packaging>pom</packaging> + <name>Amdatu Test</name> + <description>Test project</description> + + <properties> + <!-- Version of dependend components --> + <amdatu.core.version>0.2.1</amdatu.core.version> + <amdatu.web.version>0.2.1</amdatu.web.version> + <amdatu.libraries.version>0.2.0</amdatu.libraries.version> + <amdatu.cassandra.version>0.2.2-SNAPSHOT</amdatu.cassandra.version> + <amdatu.opensocial.version>0.2.0-RC1</amdatu.opensocial.version> + </properties> + + <modules> + <module>profile-service</module> + <module>profile-gadget</module> + </modules> + + <reporting> + <plugins> + <plugin> + <artifactId>maven-checkstyle-plugin</artifactId> + <version>2.6</version> + <configuration> + <!-- Aggregation is not yet supported in 2.6. A highly popular JIRA issue has + been created to resolve this issue: http://jira.codehaus.org/browse/MCHECKSTYLE-62 + <aggregate>true</aggregate> + --> + + <linkXref>true</linkXref> + <configLocation>target/amdatu_checks.xml</configLocation> + <enableRulesSummary>false</enableRulesSummary> + </configuration> + </plugin> + </plugins> + </reporting> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-antrun-plugin</artifactId> + <version>1.6</version> + <dependencies> + <dependency> + <groupId>ant-contrib</groupId> + <artifactId>ant-contrib</artifactId> + <version>1.0b3</version> + <exclusions> + <exclusion> + <groupId>ant</groupId> + <artifactId>ant</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.apache.ant</groupId> + <artifactId>ant-nodeps</artifactId> + <version>1.8.1</version> + </dependency> + </dependencies> + <executions> + <execution> + <phase>install</phase> + <goals> + <goal>run</goal> + </goals> + <configuration> + <target> + <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref="maven.compile.classpath" /> + <available file="${project.build.directory}/${project.build.finalName}.jar" property="fileExists" value="true" /> + <if> + <isset property="fileExists" /> + <then> + <if> + <isset property="amdatu.deploy.directory" /> + <then> + <copy file="${project.build.directory}/${project.build.finalName}.jar" tofile="${amdatu.deploy.directory}/${project.artifactId}-${project.version}.jar" overwrite="true" /> + </then> + <else> + <echo message="Property amdatu.deploy.directory not defined, skipping copy to deploy dir task" /> + </else> + </if> + </then> + </if> + </target> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> + +</project> \ No newline at end of file Added: sandbox/matthijsh/cassandra-example/profile-service/pom.xml ============================================================================== --- (empty file) +++ sandbox/matthijsh/cassandra-example/profile-service/pom.xml Thu Dec 22 14:54:03 2011 @@ -0,0 +1,144 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- Copyright (c) 2010, 2011 The Amdatu Foundation 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.verning + permissions and limitations under the License. --> +<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/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.amdatu.cassandra</groupId> + <artifactId>org.amdatu.cassandra.example</artifactId> + <version>0.2.2-SNAPSHOT</version> + </parent> + <artifactId>org.amdatu.cassandra.example.profile.service</artifactId> + <packaging>bundle</packaging> + <name>Amdatu Cassandra - Profile Example Service</name> + <description>Provides an example implementation of a profile store</description> + + <properties> + <amdatu.auth.version>0.2.0</amdatu.auth.version> + </properties> + + <dependencies> + <dependency> + <groupId>org.amdatu.cassandra</groupId> + <artifactId>org.amdatu.cassandra.listener</artifactId> + <scope>provided</scope> + <type>bundle</type> + </dependency> + <dependency> + <groupId>org.amdatu.cassandra</groupId> + <artifactId>org.amdatu.cassandra.application</artifactId> + <type>bundle</type> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.amdatu.cassandra</groupId> + <artifactId>org.amdatu.cassandra.persistencemanager</artifactId> + <type>bundle</type> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.amdatu.cassandra</groupId> + <artifactId>org.amdatu.cassandra.persistencemanager.hector</artifactId> + <type>bundle</type> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.amdatu.core</groupId> + <artifactId>org.amdatu.core.tenant</artifactId> + <type>bundle</type> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.amdatu.cassandra</groupId> + <artifactId>org.amdatu.cassandra.test.unit</artifactId> + <version>0.2.1-SNAPSHOT</version> + <type>jar</type> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.amdatu.cassandra</groupId> + <artifactId>org.amdatu.cassandra.test.unit.framework.hector</artifactId> + <type>jar</type> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.amdatu.web</groupId> + <artifactId>org.amdatu.web.jaxrs</artifactId> + <type>bundle</type> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.json</groupId> + <artifactId>json</artifactId> + <version>20090211</version> + <scope>compile</scope> + </dependency> + + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>servlet-api</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.amdatu.libraries</groupId> + <artifactId>org.amdatu.libraries.utilities</artifactId> + <version>${amdatu.libraries.version}</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <artifactId>maven-source-plugin</artifactId> + </plugin> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <configuration> + <instructions> + <Bundle-Activator>org.amdatu.cassandra.example.profile.service.osgi.Activator</Bundle-Activator> + <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName> + <Embed-Dependency>*;scope=compile</Embed-Dependency> + <Embed-Transitive>true</Embed-Transitive> + <Import-Package> + * + </Import-Package> + <Export-Package> + org.amdatu.cassandra.example.profile.service + </Export-Package> + </instructions> + </configuration> + </plugin> + </plugins> + </build> + + <profiles> + <profile> + <!-- Profile that will copy the jar to the configured amdatu.deploy.directory + when the deploy profile is activated --> + <id>deploy</id> + <activation> + <activeByDefault>false</activeByDefault> + </activation> + <build> + <defaultGoal>install</defaultGoal> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-antrun-plugin</artifactId> + </plugin> + </plugins> + </build> + </profile> + </profiles> +</project> \ No newline at end of file Added: sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/Profile.java ============================================================================== --- (empty file) +++ sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/Profile.java Thu Dec 22 14:54:03 2011 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2010, 2011 The Amdatu Foundation + * + * 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.amdatu.cassandra.example.profile.service; + +import java.util.Map; + +public interface Profile { + + String getId(); + + void setId(String id); + + String getName(); + + void setName(String name); + + String getEmailAddress(); + + void setEmailAddress(String emailAddress); + + Map<String, String> getProperties(); + + void setProperties(Map<String, String> properties); +} Added: sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/ProfileService.java ============================================================================== --- (empty file) +++ sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/ProfileService.java Thu Dec 22 14:54:03 2011 @@ -0,0 +1,14 @@ +package org.amdatu.cassandra.example.profile.service; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.Response; + +public interface ProfileService +{ + + String status(); + + Response createProfile(HttpServletRequest request); + + Response retrieveProfile(HttpServletRequest request); +} Added: sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/ProfileStore.java ============================================================================== --- (empty file) +++ sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/ProfileStore.java Thu Dec 22 14:54:03 2011 @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2010, 2011 The Amdatu Foundation 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.amdatu.cassandra.example.profile.service; + +import static org.amdatu.cassandra.example.profile.service.service.ProfileColumnFamilyProviderImpl.*; + +import java.util.Map; + +import org.amdatu.cassandra.example.profile.service.ProfileStore; +import org.amdatu.cassandra.example.profile.service.service.ProfileImpl; +import org.amdatu.cassandra.persistencemanager.CassandraPersistenceManager; + +/** + * @author ivol + * @author manuels + * @author matthijsh + */ +public class ProfileStore +{ + + private CassandraPersistenceManager persistenceMan; + + public ProfileStore(CassandraPersistenceManager pm) { + if (pm == null) + throw new NullPointerException("Persistence manager should not be null"); + persistenceMan = pm; + } + + /** + * Returns the profile stored with the given id as rowKey + * + * @param id The id of the profile to be requested + * @return The profile identified with id. If no such profile exists null will be returned. + */ + @SuppressWarnings("unchecked") + public Profile getProfile(String id) { + Profile profile = new ProfileImpl(); // create a new profile + + // retrieve the stored values via the CassandraPersistenceManager + if (persistenceMan.exists(CF_PROFILE, id)) { // If a profile with this id exists + String name = persistenceMan.getValue(CF_PROFILE, id, null, C_NAME, String.class); + String email = persistenceMan.getValue(CF_PROFILE, id, null, C_EMAIL, String.class); + Map<String, String> properties = persistenceMan.getValue(CF_PROFILE, id, null, C_PROPERTIES, Map.class); + // set the retrieved attributes + profile.setId(id); + profile.setName(name); + profile.setEmailAddress(email); + profile.setProperties(properties); + } + else + profile = null; + return profile; + } + + /** + * Saves a profile with the cassandra persistence manager + * + * @param profile The profile to be saved. It must have at least an id + * @throws ProfileStoreException + */ + public void saveProfile(Profile profile) throws ProfileStoreException { + + if (profile.getId() == null || profile.getId().isEmpty()) + throw new ProfileStoreException("The profile has no ID"); + + persistenceMan.setValue(CF_PROFILE, profile.getId(), null, C_NAME, profile.getName()); + persistenceMan.setValue(CF_PROFILE, profile.getId(), null, C_EMAIL, profile.getEmailAddress()); + persistenceMan.setValue(CF_PROFILE, profile.getId(), null, C_PROPERTIES, profile.getProperties()); + } + + /** + * Deletes the profile in the + * + * @param profile The profile to delete. + */ + public void deleteProfile(Profile profile) { + persistenceMan.deleteRow(CF_PROFILE, profile.getId()); + } + +} \ No newline at end of file Added: sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/ProfileStoreException.java ============================================================================== --- (empty file) +++ sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/ProfileStoreException.java Thu Dec 22 14:54:03 2011 @@ -0,0 +1,17 @@ +/** + * + */ +package org.amdatu.cassandra.example.profile.service; + +/** + * Simple exception class for the profile store example + * @author manuels + */ +public class ProfileStoreException extends Exception { + + private static final long serialVersionUID = -8578477228464479874L; + + public ProfileStoreException (final String msg) { + super(msg); + } +} Added: sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/ResponseException.java ============================================================================== --- (empty file) +++ sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/ResponseException.java Thu Dec 22 14:54:03 2011 @@ -0,0 +1,19 @@ +package org.amdatu.cassandra.example.profile.service; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; + +public class ResponseException extends WebApplicationException +{ + + private static final long serialVersionUID = -5709844783999352894L; + + public ResponseException(Status status, Exception e) { + this(status,e.getMessage()); + } + + public ResponseException(Status status, String msg) { + super(Response.status(status).entity(msg).type("text/plain").build()); + } +} Added: sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/bean/ProfileBean.java ============================================================================== --- (empty file) +++ sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/bean/ProfileBean.java Thu Dec 22 14:54:03 2011 @@ -0,0 +1,86 @@ +package org.amdatu.cassandra.example.profile.service.bean; + +import java.util.Map; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; + +import org.amdatu.cassandra.example.profile.service.Profile; +import org.amdatu.libraries.utilities.xml.StringMapAdapter; + +@XmlRootElement(name = "profile") +@XmlAccessorType(XmlAccessType.NONE) +public class ProfileBean implements Profile +{ + + @XmlAttribute(name = "id") + private String m_id; + + @XmlAttribute(name = "name") + private String m_name; + + @XmlAttribute(name = "email") + private String m_email; + + @XmlJavaTypeAdapter(StringMapAdapter.class) + @XmlElement(name = "properties") + private Map<String, String> m_properties; + + public ProfileBean() {} + + public ProfileBean(String id, String name, String email, Map<String, String> properties) { + setId(id); + setName(name); + setEmailAddress(email); + setProperties(properties); + } + + public ProfileBean(Profile profile) { + this(profile.getId(),profile.getName(),profile.getEmailAddress(),profile.getProperties()); + } + + @Override + public String getId() { + return m_id; + } + + @Override + public void setId(String id) { + this.m_id = id; + } + + @Override + public String getName() { + return m_name; + } + + @Override + public void setName(String name) { + this.m_name = name; + } + + @Override + public String getEmailAddress() { + return m_email; + } + + @Override + public void setEmailAddress(String emailAddress) { + this.m_email = emailAddress; + } + + @Override + public Map<String, String> getProperties() { + return m_properties; + } + + @Override + public void setProperties(Map<String, String> properties) { + this.m_properties = properties; + } + +} Added: sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/osgi/Activator.java ============================================================================== --- (empty file) +++ sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/osgi/Activator.java Thu Dec 22 14:54:03 2011 @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2010, 2011 The Amdatu Foundation + * + * 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.amdatu.cassandra.example.profile.service.osgi; + +import org.amdatu.cassandra.example.profile.service.ProfileService; +import org.amdatu.cassandra.example.profile.service.service.ProfileColumnFamilyProviderImpl; +import org.amdatu.cassandra.example.profile.service.service.ProfileServiceImpl; +import org.amdatu.cassandra.listener.ColumnFamilyAvailable; +import org.amdatu.cassandra.listener.ColumnFamilyProvider; +import org.amdatu.cassandra.persistencemanager.CassandraPersistenceManager; +import org.amdatu.libraries.utilities.osgi.ServiceDependentActivator; +import org.amdatu.web.rest.jaxrs.JaxRsSpi; +import org.apache.felix.dm.DependencyManager; +import org.osgi.framework.BundleContext; +import org.osgi.service.log.LogService; + +/** + * This class implements the OSGi Activator of this bundle. It is responsible for the registration of + * all service components contained by this bundle. + */ +public class Activator extends ServiceDependentActivator { + + + @Override + protected Class<?>[] getRequiredServices() { + return new Class<?>[] { JaxRsSpi.class }; + } + + @Override + public void initWithDependencies(BundleContext context, DependencyManager manager) throws Exception { + // To append a column family to Cassandra, we need to register a service implementing + // the ColumnFamilyProvider interface. The implementation defines the properties of + // that ColumnFamily, like the type (Super or Standard) and the keyspace(s) the ColumnFamily + // should be added too. + manager.add(createComponent() + .setInterface(new String[]{ColumnFamilyProvider.class.getName()}, null) + .setImplementation(ProfileColumnFamilyProviderImpl.class)); + + // Create and register the profile service. The profile service will store its data in + // the Profile ColumnFamily, defined by the ProfileColumnFamilyProviderImpl service component + // registered above. This ColumnFamily defines that this ColumnFamily should only be added + // the Default keyspace. For that reason, it needs to use the Cassandra Persistence Manager + // for that particular keyspace (Cassandra Persistence Managers are available for each keyspace). + // Therefore we define a keyspace filter which we will use to define a service dependency + // on this specific persistence manager. + String keyspaceFilter = "(" + CassandraPersistenceManager.KEYSPACE_AWARE_KEY + "=" + + CassandraPersistenceManager.DEFAULT_KEYSPACE + ")"; + + // The profile service stores its data in the Profile ColumnFamily. That means that this + // service can only do its work if that ColumnFamily is available. The ColumnFamily is created + // asynchronously by registration of the ColumnFamilyProvider service above. To ensure + // that this service does not run before this ColumnFamily is available, we can define + // a service dependency with the ColumnFamilyAvailable service, provided with a filter + // for our specific Profile ColumnFamily. + String profileCFFilter = "(&" + keyspaceFilter + "(" + ColumnFamilyAvailable.FILTER_NAME + + "=" + ProfileColumnFamilyProviderImpl.CF_PROFILE + "))"; + + // Create and register the profile service + manager.add( + createComponent() + .setImplementation(ProfileServiceImpl.class) + .setInterface(ProfileService.class.getName(), null) + .add(createServiceDependency().setService(LogService.class).setRequired(true)) + .add(createServiceDependency().setService(CassandraPersistenceManager.class, keyspaceFilter).setRequired(true)) + .add(createServiceDependency().setService(ColumnFamilyAvailable.class, profileCFFilter).setRequired(true))); + } + + @Override + public void destroy(final BundleContext context, final DependencyManager manager) throws Exception { + } +} + + Added: sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/service/ProfileColumnFamilyProviderImpl.java ============================================================================== --- (empty file) +++ sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/service/ProfileColumnFamilyProviderImpl.java Thu Dec 22 14:54:03 2011 @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2010, 2011 The Amdatu Foundation + * + * 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.amdatu.cassandra.example.profile.service.service; + +import org.amdatu.cassandra.listener.ColumnFamilyDefinition; +import org.amdatu.cassandra.listener.ColumnFamilyDefinition.ColumnType; +import org.amdatu.cassandra.listener.ColumnFamilyDefinition.CompareType; +import org.amdatu.cassandra.listener.ColumnFamilyProvider; +import org.amdatu.cassandra.persistencemanager.CassandraPersistenceManager; + +/** + * This service provides the ColumnFamily 'Profile'. + * + * @author ivol + */ +public class ProfileColumnFamilyProviderImpl implements ColumnFamilyProvider { + /** + * Name of the ColumnFamily that stores the profiles. + */ + public static final String CF_PROFILE = "ExampleProfile"; + + // Column families of this ColumnFamily. Note that we will store all values as + // String, even though the properties attribute is not of type String but of + // type Map<String, String>. The advantage of storing all columns with the same + // data type is that it is much easier to retrieve all columns at once + public static final String C_NAME = "name"; + public static final String C_EMAIL = "email"; + public static final String C_PROPERTIES = "properties"; + + @Override + public ColumnFamilyDefinition[] getColumnFamilies() { + return new ColumnFamilyDefinition[] { + new ColumnFamilyDefinition( + // Name of the ColumnFamily + CF_PROFILE, + + // The keyspaces to add the ColumnFamily to. May be null, in which case the + // ColumnFamily is appended to all keyspaces (including keyspaces created later on). + // We append this ColumnFamily only to the Default keyspace, which is always available. + new String[] {CassandraPersistenceManager.DEFAULT_KEYSPACE}, + + // The type of the ColumnFamily, may be SUPER or STANDARD. We define STANDARD. + ColumnType.STANDARD, + + // The comparator type for its columns + CompareType.BYTESTYPE, + + // Since this is a STANDARD ColumnFamily, we should not provide a comparator for + // super columns, as it has none. + null)}; + } +} \ No newline at end of file Added: sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/service/ProfileImpl.java ============================================================================== --- (empty file) +++ sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/service/ProfileImpl.java Thu Dec 22 14:54:03 2011 @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2010, 2011 The Amdatu Foundation 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.amdatu.cassandra.example.profile.service.service; + +import java.util.Map; + +import org.amdatu.cassandra.example.profile.service.Profile; + +/** + * This class implements a bean holding all properties of a Profile. + */ +public class ProfileImpl implements Profile +{ + private String id; + private String name; + private String emailAddress; + private Map<String, String> properties; + + public ProfileImpl() {} + + public ProfileImpl(String id, String name, String email, Map<String, String> properties) { + setId(id); + setName(name); + setEmailAddress(email); + setProperties(properties); + } + + @Override + public String getId() { + return id; + } + + @Override + public void setId(String id) { + this.id = id; + } + + @Override + public String getName() { + return name; + } + + @Override + public void setName(String name) { + this.name = name; + } + + @Override + public String getEmailAddress() { + return emailAddress; + } + + @Override + public void setEmailAddress(String emailAddress) { + this.emailAddress = emailAddress; + } + + @Override + public Map<String, String> getProperties() { + return properties; + } + + @Override + public void setProperties(Map<String, String> properties) { + this.properties = properties; + } + + public boolean equals(Profile secondProfile) { + if (this.id.equals(secondProfile.getId()) && this.name.equals(secondProfile.getName()) && this.emailAddress.equals(secondProfile.getEmailAddress()) && this.properties.equals(secondProfile.getProperties())) { + return true; + } + else { + return false; + } + } +} Added: sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/service/ProfileServiceImpl.java ============================================================================== --- (empty file) +++ sandbox/matthijsh/cassandra-example/profile-service/src/main/java/org/amdatu/cassandra/example/profile/service/service/ProfileServiceImpl.java Thu Dec 22 14:54:03 2011 @@ -0,0 +1,135 @@ +package org.amdatu.cassandra.example.profile.service.service; + +import static javax.ws.rs.core.MediaType.APPLICATION_JSON; +import static javax.ws.rs.core.MediaType.TEXT_PLAIN; +import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; +import static org.osgi.service.log.LogService.LOG_ERROR; +import static org.osgi.service.log.LogService.LOG_INFO; + +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.CacheControl; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; + +import org.amdatu.cassandra.example.profile.service.Profile; +import org.amdatu.cassandra.example.profile.service.ProfileService; +import org.amdatu.cassandra.example.profile.service.ProfileStore; +import org.amdatu.cassandra.example.profile.service.ProfileStoreException; +import org.amdatu.cassandra.example.profile.service.ResponseException; +import org.amdatu.cassandra.example.profile.service.bean.ProfileBean; +import org.amdatu.cassandra.persistencemanager.CassandraPersistenceManager; +import org.osgi.service.log.LogService; + +/** + * @author matthijsh + */ +@Path("profile") +public class ProfileServiceImpl implements ProfileService +{ + + // Service dependencies, injected by the Felix dependency manager + private volatile CassandraPersistenceManager m_persistenceMan; + private volatile LogService m_logService; + + private ProfileStore store; + + // Disable HTTP caching in this REST interface + private static CacheControl NO_CACHE_CONTROL; + static { + NO_CACHE_CONTROL = new CacheControl(); + NO_CACHE_CONTROL.setNoCache(true); + } + + public ProfileServiceImpl() { } + + public void start() { + store = new ProfileStore(m_persistenceMan); + } + + /** + * This method can be used to check the availability of the Profile Service. + * + * @return "Profile service for tenant online" + */ + @GET + @Produces({ TEXT_PLAIN }) + @Override + public String status() { + return "Profile service for tenant online"; + } + + /** + * This REST service provides the functionality to add a profile to the ProfileStore. + * + * @param request The request should contain at least the parameters <i>id</i>, <i>name</i> and + * <i>email</i>. + * @return A {@link ProfileBean} in case of no error + * @see ProfileStore#saveProfile(Profile) + */ + @POST + @Path("create") + @Produces({ APPLICATION_JSON }) + @Override + public Response createProfile(@Context HttpServletRequest request) { + String id = java.util.UUID.randomUUID().toString(); + String name = request.getParameter("name"); + String email = request.getParameter("email"); + Map<String, String> props = new HashMap<String, String>(); + + Profile profile = new ProfileImpl(id, name, email, props); + try { + store.saveProfile(profile); + m_logService.log(LOG_INFO, "Profile with id " + id + " saved"); + } + catch (ProfileStoreException pse) { + m_logService.log(LOG_ERROR, "Could not save profile", pse); + throw new ResponseException(INTERNAL_SERVER_ERROR, pse); + } + return responseOk(new ProfileBean(profile)); + } + + /** + * This REST service provides the functionality to retrieve a specific profile. + * + * @param request The request should contain at least the parameter <i>id</i> + * @return A {@link ProfileBean} in case of no error + * @see ProfileStore#getProfile(String) + */ + @POST + @Path("retrieve") + @Produces({ APPLICATION_JSON }) + @Override + public Response retrieveProfile(@Context HttpServletRequest request) { + String id = request.getParameter("id"); + Profile profile = store.getProfile(id); // Retrieve the profile + + if (profile != null) { + m_logService.log(LOG_INFO, "Found profile with id " + id); + return responseOk(new ProfileBean(profile)); + } + else { + m_logService.log(LOG_ERROR, "Could not find profile with id " + id); + throw new ResponseException(INTERNAL_SERVER_ERROR, "Could not find profile"); + } + } + + // + + /** + * Creates and returns a default OK response with no cache control and the specified data + * included. + * + * @param o The object (data) to include + * @return The OK response + */ + public static Response responseOk(Object o) { + return Response.ok(o).cacheControl(NO_CACHE_CONTROL).build(); + } +} _______________________________________________ Amdatu-commits mailing list [email protected] http://lists.amdatu.org/mailman/listinfo/amdatu-commits
