vre-modeler/src/org/gcube/vremanagement/vremodeler/impl/ModelFactoryService.java

148 lines
3.8 KiB
Java
Raw Normal View History

package org.gcube.vremanagement.vremodeler.impl;
import java.sql.ResultSet;
import java.util.ArrayList;
import org.apache.axis.components.uuid.UUIDGen;
import org.apache.axis.components.uuid.UUIDGenFactory;
import org.apache.axis.message.addressing.EndpointReferenceType;
import org.gcube.common.core.faults.GCUBEFault;
import org.gcube.common.core.types.VOID;
import org.gcube.common.core.utils.logging.GCUBELog;
import org.gcube.vremanagement.vremodeler.db.DBInterface;
import org.gcube.vremanagement.vremodeler.impl.util.XMLUtil;
import org.gcube.vremanagement.vremodeler.stubs.GetExistingNamesResponseMessage;
public class ModelFactoryService{
private GCUBELog logger = new GCUBELog(ModelFactoryService.class);
private static final UUIDGen uuidGen = UUIDGenFactory.getUUIDGen();
protected ServiceContext getServiceContext() {
return ServiceContext.getContext();
}
/**
* implementation of createResource method
* @param request creation request
* @return the EndpointReference pointing to the resource
* @throws GCUBEFault if something fails
*/
public EndpointReferenceType createResource() throws GCUBEFault {
String id=uuidGen.nextUUID();
logger.trace("resource "+id+" created");
ModelerResource mr;
try{
ModelerContext pctx= ModelerContext.getPortTypeContext();
mr=(ModelerResource)pctx.getWSHome().create(pctx.makeKey(id), id);
mr.store();
return mr.getEPR();
}catch (Exception e){logger.error("error creating resource",e); throw new GCUBEFault(e);}
}
/**
* return the existing DL Name
*
* @param request void
* @return array of string with existing VRE names
* @throws RemoteException -
*/
public GetExistingNamesResponseMessage getExistingNamesVREs(VOID arg) throws GCUBEFault {
ArrayList<String> toReturn= new ArrayList<String>();
ResultSet res=null;
try{
DBInterface.connect();
res= DBInterface.queryDB("select VRE.name from VRE;");
while (res.next())
toReturn.add(res.getString(1));
}catch(Exception e) {logger.error("error on DB"); throw new GCUBEFault(e);}
GetExistingNamesResponseMessage response = new GetExistingNamesResponseMessage();
response.setNames(toReturn.toArray(new String[0]));
return response;
}
/**
* Initialize the DB
*
* @param request void
* @return void
* @throws GCUBEFault thrown if something fails
*/
public void initDB(VOID arg) throws GCUBEFault {
logger.debug("initDB method");
Thread t= new Thread(){
public void run(){
try{
ServiceContext.getContext().intializeDB();
}catch(Exception e){
logger.error("DB inizialization failed"+e);
e.printStackTrace();
}
}
};
t.start();
}
/**
*
* @param request void
* @return String
* @throws RemoteException -
*/
public String getAllVREs(VOID arg) throws GCUBEFault{
String toReturn = null;
ResultSet res=null;
try{
DBInterface.connect();
res= DBInterface.queryDB("select VRE.name, VRE.description, VRE.status, VRE.epr from VRE;");
}catch(Exception e) {logger.error("VREModel: error on DB"); throw new GCUBEFault(e);}
try {
toReturn=XMLUtil.PrepareAllVREsXML(res);
} catch (Exception e) {
logger.error("VREModel parsing error in function getAllDLs "+e.getMessage());
throw new GCUBEFault(e);
}
return toReturn;
}
/**
*
* remove the DL instance
*
* @param request the id of VRE to remove
* @return void
* @throws RemoteException -
*/
public VOID removeVRE(String id) throws GCUBEFault{
logger.trace("Deleting resource with id "+id);
try{
ModelerContext pctx= ModelerContext.getPortTypeContext();
//destroy the resource;
DBInterface.ExecuteUpdate("DELETE FROM VRE where VRE.id='"+id+"';");
pctx.getWSHome().remove(pctx.makeKey(id));
return new VOID();
}catch(Exception e){
logger.error("error removing resource",e);
throw new GCUBEFault(e);
}
}
}