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.porttypes.GCUBEStartupPortType; 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; import org.globus.wsrf.tests.basic.CreateResource; public class ModelFactoryService extends GCUBEStartupPortType{ private GCUBELog logger = new GCUBELog(ModelFactoryService.class.getName()); private static final UUIDGen uuidGen = UUIDGenFactory.getUUIDGen(); @Override 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(CreateResource request) 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); 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 toReturn= new ArrayList(); 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("VRE Model: 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 DL to remove * @return void * @throws RemoteException - */ public void removeVRE(String request) throws GCUBEFault{ try{ ResultSet res=DBInterface.queryDB("Select VRE.id from VRE where VRE.epr='"+request+"';"); String id; if (res.next()){ try{ id= res.getString(1); }catch(Exception e){logger.error("Error retrieving the VRE on DB "+e); throw e;} }else throw new Exception("VRE not retreived in DB"); ModelerContext pctx= ModelerContext.getPortTypeContext(); ModelerResource mr= (ModelerResource) pctx.getWSHome().find(pctx.makeKey(id)); logger.trace("Deleting resource with id "+id); System.out.println("Deleting resource "+id); //destroy the resource; DBInterface.ExecuteUpdate("DELETE FROM VRE where VRE.id='"+id+"';"); mr.remove(); }catch(Exception e){ logger.error("VDLModel: "+e.getMessage()); throw new GCUBEFault( e); } } }