/** Add a deployed assembly */ void add( DeployedAssembly assemblyStateFromFileSystem, List<DeployedComponent> componentsDeployment) throws PersistenceException { if (LOG.isDebugEnabled()) LOG.debug( "Persisting assembly: " + assemblyStateFromFileSystem + ", components: " + componentsDeployment); String name = assemblyStateFromFileSystem.getAssemblyId().getAssemblyName(); int version = assemblyStateFromFileSystem.getAssemblyId().getAssemblyVersion(); String adir = _deployDir .toURI() .relativize((new File(assemblyStateFromFileSystem.getAssemblyDir())).toURI()) .getPath(); boolean active = true; Connection c = null; try { c = getConnection(); // Insert assembly if (LOG.isDebugEnabled()) LOG.debug("INSERT_DEPLOYMENT_ASSEMBLY: " + name + "-" + version); EasyStatement inserta = new EasyStatement(c, "INSERT INTO DEPLOY_ASSEMBLIES VALUES (?,?,?,?)"); try { inserta.write(name); inserta.write(version); inserta.write(adir); inserta.write(active ? 1 : 0); inserta.execute(); } finally { inserta.close(); } // Insert components for (DeployedComponent dc : assemblyStateFromFileSystem.getDeployedComponents()) { File assemblyDir = new File(assemblyStateFromFileSystem.getAssemblyDir()); String component = dc.getComponentId().getComponentName(); String cdir = assemblyDir.toURI().relativize(new File(dc.getComponentDir()).toURI()).getPath(); String manager = dc.getComponentManagerName(); if (LOG.isDebugEnabled()) LOG.debug("INSERT_DEPLOYMENT_COMPONENT: " + name + "-" + version + "/" + component); EasyStatement insertc = new EasyStatement(c, "INSERT INTO DEPLOY_COMPONENTS VALUES (?,?,?,?,?)"); try { insertc.write(name); insertc.write(version); insertc.write(component); insertc.write(manager); insertc.write(cdir); insertc.execute(); } finally { insertc.close(); } } // Insert resources for (DeployedComponent component : componentsDeployment) { // only if the file system says the component is deployed if (assemblyStateFromFileSystem.getDeployedComponents().contains(component)) { for (String resource : component.getDeployedResources()) { if (LOG.isDebugEnabled()) LOG.debug("INSERT_DEPLOYMENT_RESOURCE: " + name + "-" + version + "/" + component); EasyStatement stmt = new EasyStatement( c, "INSERT INTO DEPLOY_RESOURCES(ASSEMBLY, VERSION, COMPONENT, MANAGER, RESOURCE_ID) VALUES(?, ?, ?, ?, ?)"); stmt.write(component.getComponentId().getAssemblyId().getAssemblyName()); stmt.write(component.getComponentId().getAssemblyId().getAssemblyVersion()); stmt.write(component.getComponentId().getComponentName()); stmt.write(component.getComponentManagerName()); stmt.write(resource); stmt.execute(); stmt.close(); } } } } catch (SQLException e) { throw new PersistenceException(e); } finally { close(c); } }
/** Load persistent deployed state */ Map<AssemblyId, DeployedAssembly> load() { Map<AssemblyId, DeployedAssembly> assembliesById = new HashMap<AssemblyId, DeployedAssembly>(); Map<TypedComponentId, List<String>> resourceListsByComponentId = new HashMap<TypedComponentId, List<String>>(); Connection c = null; EasyResultSet rs = null; try { c = getConnection(); EasyStatement selecta = new EasyStatement(c, "SELECT * FROM DEPLOY_ASSEMBLIES"); try { rs = selecta.executeQuery(); while (rs.next()) { String assembly = rs.readString(); int version = rs.readInt(); String adir = rs.readString(); boolean active = rs.readInt() != 0; AssemblyId aid = new AssemblyId(assembly, version); adir = (new File(_deployDir, adir)).toString(); DeployedAssembly da = new DeployedAssembly(aid, adir, new ArrayList<DeployedComponent>(), active); assembliesById.put(aid, da); } } finally { if (rs != null) rs.close(); selecta.close(); } EasyStatement selectb = new EasyStatement(c, "SELECT * FROM DEPLOY_RESOURCES"); try { rs = selectb.executeQuery(); while (rs.next()) { String assembly = rs.readString(); int version = rs.readInt(); String component = rs.readString(); String type = rs.readString(); String resource = rs.readString(); TypedComponentId tcid = new TypedComponentId(assembly, version, component, type); List<String> deployedResource = resourceListsByComponentId.get(tcid); if (deployedResource == null) { deployedResource = new ArrayList<String>(); resourceListsByComponentId.put(tcid, deployedResource); } deployedResource.add(resource); } } finally { if (rs != null) rs.close(); selectb.close(); } EasyStatement selectc = new EasyStatement(c, "SELECT * FROM DEPLOY_COMPONENTS"); try { rs = selectc.executeQuery(); while (rs.next()) { String assembly = rs.readString(); int version = rs.readInt(); String component = rs.readString(); String type = rs.readString(); String cdir = rs.readString(); AssemblyId aid = new AssemblyId(assembly, version); ComponentId cid = new ComponentId(aid, component); DeployedAssembly da = assembliesById.get(aid); if (da == null) { LOG.error("Deployed component entry is missing parent assembly: " + cid); } else { cdir = (new File(da.getAssemblyDir(), cdir)).toString(); List<String> deployedResources = resourceListsByComponentId.get(new TypedComponentId(cid, type)); if (deployedResources == null) { deployedResources = new ArrayList<String>(); } DeployedComponent dc = new DeployedComponent(cid, cdir, type, deployedResources); da.getDeployedComponents().add(dc); if (LOG.isDebugEnabled()) LOG.debug( "DEPLOYMENT.component discovered: " + dc + ", resources = " + dc.getDeployedResources()); } } } finally { if (rs != null) rs.close(); selectc.close(); } return assembliesById; } catch (SQLException e) { throw new PersistenceException(e); } finally { close(c); } }