Commit 32c955bd authored by Alessio Carenini's avatar Alessio Carenini
Browse files

Deleted since persistence is managed by Brooklyn

parent ca5db729
/*
* CEFRIEL - Chorevolution
*
* 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.brooklyn.rest.chorevolution;
import java.io.StringReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import com.lambdaworks.redis.RedisClient;
import com.lambdaworks.redis.RedisConnection;
import com.lambdaworks.redis.RedisURI;
import eu.chorevolution.datamodel.Choreography;
/**
* Stores choreography descriptions.
*
* @author leonardo
*
*/
public class ChorRegistry {
private static ChorRegistry instance = new ChorRegistry();
private AtomicInteger counter = null;
private RedisClient redisClient = null;
private RedisConnection<String, String> connection = null;
private JAXBContext chor_context = null;
private static String chor_prefix = "chor.";
private ChorRegistry() {
String stored_id = null;
redisClient = new RedisClient(RedisURI.create("redis://localhost:6379"));
connection = redisClient.connect();
System.out.println("Connected to Redis");
try {
chor_context = JAXBContext.newInstance(Choreography.class);
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stored_id = connection.get("counter");
if (stored_id != null) {
counter = new AtomicInteger(Integer.parseInt(stored_id));
} else {
counter = new AtomicInteger();
}
}
@Override
protected void finalize() throws Throwable {
// TODO Auto-generated method stub
super.finalize();
connection.close();
redisClient.shutdown();
}
public static ChorRegistry getInstance() {
return instance;
}
/**
* Creates a new choreography entry
*
* @return the just registred choreography ID
*/
public String create(Choreography chor) {
String id = Integer.toString(counter.incrementAndGet());
String xml_dump = null;
try {
xml_dump = chor.getXML();
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connection.set(chor_prefix + id, xml_dump);
connection.set("counter", id);
return id;
}
public void addChoreography(Choreography chor) {
if (connection.get(chor_prefix + chor.getId()) != null)
throw new IllegalArgumentException("Choreography is already on registry");
else
try {
connection.set(chor_prefix + chor.getId(), chor.getXML());
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void updateChoreography(Choreography chor) {
try {
connection.set(chor_prefix + chor.getId(), chor.getXML());
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Choreography getChoreography(String chorId) {
String chor_xml = connection.get(chor_prefix + chorId);
Unmarshaller um = null;
Choreography result = null;
try {
um = chor_context.createUnmarshaller();
result = (Choreography) um.unmarshal(new StringReader(chor_xml));
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public Choreography getRequestedChoreographySpec(String chorId) {
String chorspec_xml = connection.get(chor_prefix + chorId);
Unmarshaller um = null;
Choreography result = null;
try {
um = chor_context.createUnmarshaller();
result = (Choreography) um.unmarshal(new StringReader(chorspec_xml));
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public boolean contains(String chorId) {
if (getChoreography(chorId) != null)
return true;
else
return false;
}
public Map<String, Choreography> getAll() {
Map<String, Choreography> chors = new HashMap<String, Choreography>();
List<String> keys = connection.keys(chor_prefix + "*");
;
for (String key : keys) {
chors.put(key.replace(chor_prefix, ""), getChoreography(key));
}
return chors;
}
public void clean() {
List<String> keys = connection.keys(chor_prefix + "*");
;
for (String key : keys) {
connection.del(key);
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment