Skip to content

Instantly share code, notes, and snippets.

@lahirue
Created October 21, 2015 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lahirue/c52b6c7c631ab3f6de56 to your computer and use it in GitHub Desktop.
Save lahirue/c52b6c7c631ab3f6de56 to your computer and use it in GitHub Desktop.
WSO2 Carbon Component Web Service
package org.wso2.carbon.testService;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.RegistryType;
import org.wso2.carbon.registry.api.Registry;
import org.wso2.carbon.registry.api.RegistryException;
import org.wso2.carbon.registry.api.Resource;
import org.wso2.carbon.testService.data.User;
public class MyService {
Registry registry;
List<User> userList = new ArrayList<User>();
private static final String ORDER_PATH = "order_location";
public MyService() {
registry = CarbonContext.getThreadLocalCarbonContext().getRegistry(
RegistryType.valueOf(RegistryType.LOCAL_REPOSITORY.toString()));
}
public User addUser(String aName, int anAge) {
User temp = new User(aName, anAge);
userList.add(temp);
Resource tempRes;
try {
tempRes = registry.newResource();
tempRes.setContent(serialize(userList));
registry.put(ORDER_PATH, tempRes);
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("Completed");
return temp;
}
public List<User> displayUsers() {
Resource getUserRes;
try {
getUserRes = registry.get(ORDER_PATH);
userList = (List<User>) deserialize((byte[]) getUserRes.getContent());
} catch (RegistryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return userList;
}
private static byte[] serialize(Object obj) throws IOException {
ByteArrayOutputStream b = new ByteArrayOutputStream();
ObjectOutputStream o = new ObjectOutputStream(b);
o.writeObject(obj);
return b.toByteArray();
}
private static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
ByteArrayInputStream b = new ByteArrayInputStream(bytes);
ObjectInputStream o = new ObjectInputStream(b);
return o.readObject();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment