solved a bug in Service initialization

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/vre-management/VREModeler@15671 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Lucio Lelii 2009-10-01 14:59:26 +00:00
parent 72e0ca296a
commit 33e8f6fb61
7 changed files with 35 additions and 35 deletions

View File

@ -8,7 +8,7 @@ import java.sql.DriverManager;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.List; import java.util.Hashtable;
import org.gcube.common.core.faults.GCUBEFault; import org.gcube.common.core.faults.GCUBEFault;
import org.gcube.common.core.utils.logging.GCUBELog; import org.gcube.common.core.utils.logging.GCUBELog;
import org.gcube.vremanagement.vremodeler.impl.ServiceContext; import org.gcube.vremanagement.vremodeler.impl.ServiceContext;
@ -21,8 +21,7 @@ import org.gcube.vremanagement.vremodeler.impl.util.Listable;
*/ */
public class DBInterface { public class DBInterface {
private static Hashtable< String, Connection> connectionMapping= new Hashtable<String, Connection>();
private static Connection conn;
private static String dbFile = ServiceContext.getContext().getPersistenceRoot()+ File.separator + "vreModelerDB"; private static String dbFile = ServiceContext.getContext().getPersistenceRoot()+ File.separator + "vreModelerDB";
@ -34,14 +33,14 @@ public class DBInterface {
*/ */
public static boolean dbAlreadyCreated(){ public static boolean dbAlreadyCreated(){
File file= new File(dbFile+ServiceContext.getContext().getScope().toString().replace("/", "-")+".script"); File file= new File(dbFile+ServiceContext.getContext().getScope().toString().replace("/", "-")+".data");
logger.trace("the db "+file.getName()+" exists ?"+ file.exists()); logger.trace("the db "+file.getName()+" exists ?"+ file.exists());
return file.exists(); return file.exists();
} }
public static Connection connect() throws SQLException{ public static Connection connect() throws SQLException{
if(conn==null) if(connectionMapping.get(ServiceContext.getContext().getScope().toString())==null)
{ {
// Load the HSQL Database Engine JDBC driver // Load the HSQL Database Engine JDBC driver
// hsqldb.jar should be in the class path or made part of the current jar // hsqldb.jar should be in the class path or made part of the current jar
@ -57,35 +56,36 @@ public class DBInterface {
// It can contain directory names relative to the // It can contain directory names relative to the
// current working directory // current working directory
dbFile=dbFile+ServiceContext.getContext().getScope().toString().replace("/", "-");
conn = DriverManager.getConnection("jdbc:hsqldb:file:" Connection conn = DriverManager.getConnection("jdbc:hsqldb:file:"
+ dbFile, // filenames + dbFile+ServiceContext.getContext().getScope().toString().replace("/", "-"), // filenames
"sa", // username "sa", // username
""); // password ""); // password
connectionMapping.put(ServiceContext.getContext().getScope().toString(), conn);
} }
return conn; return connectionMapping.get(ServiceContext.getContext().getScope().toString());
} }
public static Connection getConnection(){ public static Connection getConnection(){
return conn; return connectionMapping.get(ServiceContext.getContext().getScope().toString());
} }
/** /**
* *
* @param query * @param query
*/ */
public static synchronized void ExecuteUpdate(String query) throws GCUBEFault{ public static synchronized void ExecuteUpdate(String query) throws Exception{
Statement st = null; Statement st = null;
Connection conn= getConnection();
try{ try{
st = conn.createStatement(); // statement objects can be reused with st = conn.createStatement(); // statement objects can be reused with
st.executeUpdate(query); st.executeUpdate(query);
st.close(); st.close();
}catch(SQLException e){//logger.error("error executing Query", e); }catch(SQLException e){throw e;}
throw new GCUBEFault(e);}
} }
@ -95,6 +95,8 @@ public class DBInterface {
if (values==null) throw new GCUBEFault(); if (values==null) throw new GCUBEFault();
Statement st = null; Statement st = null;
Connection conn= getConnection();
try{ try{
st = conn.createStatement(); // statement objects can be reused with st = conn.createStatement(); // statement objects can be reused with
}catch(SQLException e){//logger.error("error creating SQL statement"); }catch(SQLException e){//logger.error("error creating SQL statement");
@ -146,6 +148,7 @@ public class DBInterface {
*/ */
public static synchronized void insertInto(String table, String[]... rows) throws GCUBEFault { public static synchronized void insertInto(String table, String[]... rows) throws GCUBEFault {
Connection conn= getConnection();
Statement st = null; Statement st = null;
try{ try{
@ -198,7 +201,8 @@ public class DBInterface {
Statement st = null; Statement st = null;
ResultSet rs = null; ResultSet rs = null;
Connection conn= getConnection();
st = conn.createStatement(); // statement objects can be reused with st = conn.createStatement(); // statement objects can be reused with
@ -218,12 +222,14 @@ public class DBInterface {
*/ */
public static void deleteAll(String table) throws SQLException{ public static void deleteAll(String table) throws SQLException{
String query="DELETE FROM "+table.toUpperCase(); String query="DELETE FROM "+table.toUpperCase();
Connection conn= getConnection();
Statement st=conn.createStatement(); Statement st=conn.createStatement();
st.executeUpdate(query); st.executeUpdate(query);
st.close(); st.close();
} }
public static void close() throws SQLException{ public static void close() throws SQLException{
Connection conn= getConnection();
if (conn!=null){ if (conn!=null){
conn.close(); conn.close();
conn=null; conn=null;

View File

@ -71,7 +71,6 @@ public class IStoDBUtil {
logger.trace(line); logger.trace(line);
DBInterface.ExecuteUpdate(line); DBInterface.ExecuteUpdate(line);
} }
DBInterface.close();
}catch (Exception e){ }catch (Exception e){
logger.error("error creating tables",e); logger.error("error creating tables",e);
throw new GCUBEFault(e); } throw new GCUBEFault(e); }
@ -115,7 +114,6 @@ public class IStoDBUtil {
DBInterface.deleteAll(FunctionalityHandler.portletTableName); DBInterface.deleteAll(FunctionalityHandler.portletTableName);
DBInterface.deleteAll(FunctionalityHandler.serviceTableName); DBInterface.deleteAll(FunctionalityHandler.serviceTableName);
DBInterface.deleteAll(FunctionalityHandler.functionalityTableName); DBInterface.deleteAll(FunctionalityHandler.functionalityTableName);
DBInterface.close();
}catch (SQLException e){ }catch (SQLException e){
logger.error("error cleaning sqlDB",e); logger.error("error cleaning sqlDB",e);
throw new GCUBEFault(e); } throw new GCUBEFault(e); }
@ -145,9 +143,8 @@ public class IStoDBUtil {
try{ try{
DBInterface.connect(); DBInterface.connect();
DBInterface.insertInto("NEEDEDRESOURCES", values.toArray(new String[0][0])); DBInterface.insertInto("NEEDEDRESOURCES", values.toArray(new String[0][0]));
DBInterface.close();
}catch (SQLException e){ }catch (SQLException e){
logger.error("VDLModel: DB error ",e); logger.error("error inserting Generic resource in the DB ",e);
throw new GCUBEFault(e); throw new GCUBEFault(e);
} }

View File

@ -518,8 +518,8 @@ public class ModelerService {
try{ try{
DBInterface.ExecuteUpdate("UPDATE VRE SET STATUS='"+DEPLOYING+"' WHERE VRE.id='"+resourceID+"';"); DBInterface.ExecuteUpdate("UPDATE VRE SET STATUS='"+DEPLOYING+"' WHERE VRE.id='"+resourceID+"';");
} catch (GCUBEFault e) { } catch (Exception e) {
logger.error("HSQLDB Error ",e); logger.error("DB Error ",e);
throw new GCUBEUnrecoverableFault(e); throw new GCUBEUnrecoverableFault(e);
} }

View File

@ -41,6 +41,7 @@ public class ServiceContext extends GCUBEServiceContext{
logger.info("ready event invoked on " + this.getName()); logger.info("ready event invoked on " + this.getName());
this.intializeDB(); this.intializeDB();
}catch (Exception e){ }catch (Exception e){
logger.error("error initializing VREModeler",e);
this.setStatus(Status.FAILED); this.setStatus(Status.FAILED);
throw e; throw e;
} }
@ -48,8 +49,7 @@ public class ServiceContext extends GCUBEServiceContext{
protected void intializeDB() throws Exception{ protected void intializeDB() throws Exception{
ISNotifier notifier= GHNContext.getImplementation(ISNotifier.class); ISNotifier notifier= GHNContext.getImplementation(ISNotifier.class);
for (GCUBEScope scope : ServiceContext.getContext().getStartScopes()){ for (GCUBEScope scope : ServiceContext.getContext().getInstance().getScopes().values()){
if (scope.isInfrastructure()) continue;
ServiceContext.getContext().setScope(scope); ServiceContext.getContext().setScope(scope);
GCUBESecurityManager secMan= new GCUBESecurityManagerImpl(){ GCUBESecurityManager secMan= new GCUBESecurityManagerImpl(){
@Override @Override

View File

@ -115,7 +115,7 @@ public class DeployVRE extends Thread{
} }
vreName= resGenericInfo.getString(1); vreName= resGenericInfo.getString(1);
} catch (SQLException e) { } catch (Exception e) {
logger.error("HSQLDB Error "+e); logger.error("HSQLDB Error "+e);
throw new GCUBEFault(e); throw new GCUBEFault(e);
} }

View File

@ -5,7 +5,6 @@ import java.sql.PreparedStatement;
import java.sql.Types; import java.sql.Types;
import org.gcube.common.core.contexts.GHNContext; import org.gcube.common.core.contexts.GHNContext;
import org.gcube.common.core.faults.GCUBEFault;
import org.gcube.common.core.informationsystem.client.AtomicCondition; import org.gcube.common.core.informationsystem.client.AtomicCondition;
import org.gcube.common.core.informationsystem.client.ISClient; import org.gcube.common.core.informationsystem.client.ISClient;
import org.gcube.common.core.informationsystem.client.queries.GCUBEGenericQuery; import org.gcube.common.core.informationsystem.client.queries.GCUBEGenericQuery;
@ -65,10 +64,11 @@ public class FunctionalityHandler implements ResourceHandler<KGCUBEGenericFuncti
GCUBEGenericQuery genericQuery= queryClient.getQuery(GCUBEGenericQuery.class); GCUBEGenericQuery genericQuery= queryClient.getQuery(GCUBEGenericQuery.class);
genericQuery.setExpression(query.getExpression()); genericQuery.setExpression(query.getExpression());
resource.load(new StringReader(queryClient.execute(genericQuery, ServiceContext.getContext().getScope()).get(0).toString())); resource.load(new StringReader(queryClient.execute(genericQuery, ServiceContext.getContext().getScope()).get(0).toString()));
}catch(Exception e ){logger.error("Error queryng IS",e); throw new GCUBEFault(e); } this.functionalityResourceId= resource.getID();
this.functionalityResourceId= resource.getID(); this.insert(resource);
this.insert(resource); logger.debug("functionality initialized");
logger.debug("functionality initialized"); }catch(Exception e ){logger.error("Functionality resource not found",e); logger.warn("the service will be initialized without functionalities"); }
} }
private void insert( KGCUBEGenericFunctionalityResource resource) throws Exception{ private void insert( KGCUBEGenericFunctionalityResource resource) throws Exception{
@ -90,6 +90,7 @@ public class FunctionalityHandler implements ResourceHandler<KGCUBEGenericFuncti
psFunctionality.setInt(4, rootFunctionalityId); psFunctionality.setInt(4, rootFunctionalityId);
psFunctionality.setInt(5, 0); psFunctionality.setInt(5, 0);
psFunctionality.execute(); psFunctionality.execute();
logger.trace("FUNCTIONALITY: "+functionality.getName()+" has "+functionality.getServices().size()+" services ");
for (Service service: functionality.getServices()){ for (Service service: functionality.getServices()){
psServices.setInt(1,id); psServices.setInt(1,id);
psServices.setString(2, service.getServiceName()); psServices.setString(2, service.getServiceName());

View File

@ -1,10 +1,6 @@
package org.gcube.vremanagement.vremodeler.resources.kxml; package org.gcube.vremanagement.vremodeler.resources.kxml;
import static org.gcube.common.resources.kxml.KGCUBEResource.NS; import static org.gcube.common.resources.kxml.KGCUBEResource.NS;
import org.gcube.common.core.resources.service.Dependency;
import org.gcube.common.resources.kxml.service.KServiceDependency;
import org.gcube.common.resources.kxml.service.version.VersionSpecificationParser;
import org.gcube.common.resources.kxml.utils.KStringList; import org.gcube.common.resources.kxml.utils.KStringList;
import org.gcube.vremanagement.vremodeler.resources.Functionality; import org.gcube.vremanagement.vremodeler.resources.Functionality;
import org.gcube.vremanagement.vremodeler.resources.Service; import org.gcube.vremanagement.vremodeler.resources.Service;
@ -20,7 +16,7 @@ public class KFunctionality {
case KXmlParser.START_TAG : case KXmlParser.START_TAG :
if (parser.getName().equals("Name")) d.setName(parser.nextText().trim()); if (parser.getName().equals("Name")) d.setName(parser.nextText().trim());
if (parser.getName().equals("Description")) d.setDescription(parser.nextText().trim()); if (parser.getName().equals("Description")) d.setDescription(parser.nextText().trim());
if (parser.getName().equals("Services")) d.getServices().add(KService.load(parser)); if (parser.getName().equals("Service")) d.getServices().add(KService.load(parser));
if (parser.getName().equals("Portlets")) d.setPortlets((KStringList.load("Portlets", parser))); if (parser.getName().equals("Portlets")) d.setPortlets((KStringList.load("Portlets", parser)));
break; break;
case KXmlParser.END_TAG: case KXmlParser.END_TAG: