package org.gcube.vremanagement.vremodeler.impl; import java.io.StringReader; import java.net.URL; import java.rmi.RemoteException; import java.sql.ResultSet; import java.util.ArrayList; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NameNotFoundException; import javax.naming.NamingException; import javax.xml.namespace.QName; import javax.xml.rpc.server.ServiceLifecycle; import org.apache.axis.components.uuid.UUIDGen; import org.apache.axis.components.uuid.UUIDGenFactory; import org.apache.axis.message.addressing.AttributedURI; import org.apache.axis.message.addressing.EndpointReferenceType; import org.gcube.common.core.contexts.GCUBEServiceContext; import org.gcube.common.core.faults.GCUBEFault; import org.gcube.common.core.porttypes.GCUBEStartupPortType; import org.gcube.common.core.utils.logging.GCUBELog; import org.globus.wsrf.Constants; import org.globus.wsrf.NoResourceHomeException; import org.globus.wsrf.ResourceContext; import org.globus.wsrf.ResourceContextException; import org.globus.wsrf.ResourceHome; import org.globus.wsrf.ResourceKey; import org.globus.wsrf.container.ServiceHost; import org.globus.wsrf.encoding.ObjectDeserializer; import org.globus.wsrf.impl.SimpleResourceKey; import org.globus.wsrf.tests.basic.CreateResource; import org.globus.wsrf.tests.basic.CreateResourceResponse; import org.globus.wsrf.utils.AddressingUtils; import org.gridforum.jgss.ExtendedGSSCredential; import org.oasis.wsrf.lifetime.Destroy; import org.xml.sax.InputSource; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class ModelFactoryService extends GCUBEStartupPortType{ private GCUBELog logger = new GCUBELog(this); private static final UUIDGen uuidGen = UUIDGenFactory.getUUIDGen(); @Override protected ServiceContext getServiceContext() { return ServiceContext.getContext(); } /* Implementation of createResource Operation */ public EndpointReferenceType createResource(CreateResource request) throws GCUBEFault { String id=uuidGen.nextUUID(); 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 qith exististin dl names * @throws RemoteException - */ public GetExistingNamesResponseMessage getExistingNamesDLs(GetExistingNamesRequestMessage request) throws RemoteException { GetExistingNamesResponseMessage response=new GetExistingNamesResponseMessage(); ArrayList toReturn= new ArrayList(); ResultSet res=null; ExtendedGSSCredential cred; if (isSecurityEnabled){ try { cred=credentialsListener.getCredentials(new NAL().getDefaultVO()); if (cred==null) logger.debug("null cerdentials"); logger.debug("VDLModel - initDB - with credential"); } catch (Exception e) { logger.debug("VDLModel - initDB - null credential"); cred=null; } }else cred=null; try{ DBInterface.connect(); res= DBInterface.queryDB("select DL.name from DL;"); while (res.next()) toReturn.add(res.getString(1)); String customXquery="for $query in collection(\"/db/Profiles/VDL\")//Document/Data/child::*[local-name()='Profile']/DILIGENTResource return $query/Profile/Name/string()"; String queryResult= DISHLSClient.getGeneralQueryManager(cred, factoryEPR).queryDISIC(customXquery,cred, factoryEPR); logger.debug("VDL get Name QUERY: "+queryResult); }catch(Exception e) {logger.error("VDL Model: error on DB"); throw new RemoteException("VDLModel: error on DB",e);} response.setNames(toReturn.toArray(new String[0])); return response; } /** * Initialize the DB * * @param request void * @return void * @throws RemoteException remote exception */ public VoidType initDB(VoidType request) throws RemoteException { logger.debug("initDB method"); ExtendedGSSCredential cred; if (isSecurityEnabled){ try { cred=credentialsListener.getCredentials(new NAL().getDefaultVO()); if (cred==null) logger.debug("null cerdentials"); logger.debug("VDLModel - initDB - with credential"); } catch (Exception e) { logger.debug("VDLModel - initDB - null credential"); cred=null; } }else cred=null; new InitThread(factoryEPR, cred).start(); return new VoidType(); } /** * * @param request void * @return String * @throws RemoteException - */ public String getAllDLs(GetAllDLsMessageRequest request) throws RemoteException{ String toReturn = null; ResultSet res=null; try{ DBInterface.connect(); res= DBInterface.queryDB("select DL.name, DL.description, DL.status, DL.epr from DL;"); }catch(Exception e) {logger.error("VDL Model: error on DB"); throw new RemoteException("VDLModel: error on DB",e);} try { toReturn=XMLUtil.PrepareAllDLsXML(res); } catch (Exception e) { logger.error("VDLModel parsing error in function getAllDLs "+e.getMessage()); throw new RemoteException("VDLModel parsing error in function getAllDLs",e); } return toReturn; } protected ResourceHome getInstanceResourceHome() throws NoResourceHomeException, ResourceContextException { ResourceHome home; ResourceContext ctx; logger.debug("DILIGENT: getInstanceResourceHome on ModelFactoryService"); ctx = ResourceContext.getResourceContext(); logger.debug("DILIGENT: Resource Context = " + ctx.toString() + "on getInstanceResourceHome on ModelFactoryService"); String homeLoc = Constants.JNDI_SERVICES_BASE_NAME + ctx.getService() + "/instanceHome"; try { Context initialContext = new InitialContext(); home = (ResourceHome) initialContext.lookup(homeLoc); } catch (NameNotFoundException e) { throw new NoResourceHomeException(); } catch (NamingException e) { throw new ResourceContextException("", e); } return home; } /** * * remove the DL instance * * @param request the id of DL to remove * @return void * @throws RemoteException - */ public RemoveDLResponseMessage removeDL(String request) throws RemoteException{ ResourceManager rMan; try{ ResultSet res=DBInterface.queryDB("Select DL.epr from DL where DL.id='"+request+"';"); EndpointReferenceType dlEpr; if (res.next()){ StringReader stringReader = new StringReader(res.getString(1)); InputSource inputSource = new InputSource(stringReader); dlEpr = (EndpointReferenceType) ObjectDeserializer.deserialize(inputSource, EndpointReferenceType.class); }else throw new Exception("DL not retreived in DB"); ModelServiceAddressingLocator msal=new ModelServiceAddressingLocator(); ModelPortType vdlModelPT=msal.getModelPortTypePort(dlEpr); if (ModelFactoryService.isSecurityEnabled){ ExtendedGSSCredential cred=org.gcube.vremanagement.vremodeler.impl.credentialsListener.getCredentials((new NAL()).getDefaultVO()); rMan= DISIP.getResourceManager(cred,dlEpr); ConfigureSecurity.setSecurity(((javax.xml.rpc.Stub) vdlModelPT), cred); }else rMan= DISIP.getResourceManager(null,dlEpr); rMan.remove(dlEpr); vdlModelPT.destroy(new Destroy()); DBInterface.ExecuteUpdate("DELETE FROM DL where DL.id='"+request+"';"); }catch(Exception e){ logger.error("VDLModel: "+e.getMessage()); throw new RemoteException(e.getMessage(), e);} return new RemoveDLResponseMessage(); } }