2012-12-19 13:00:57 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* This software is part of the gCube Project.
|
|
|
|
* Site: http://www.gcube-system.org/
|
|
|
|
****************************************************************************
|
|
|
|
* The gCube/gCore software is licensed as Free Open Source software
|
|
|
|
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
|
|
|
|
* The software and documentation is provided by its authors/distributors
|
|
|
|
* "as is" and no expressed or
|
|
|
|
* implied warranty is given for its use, quality or fitness for a
|
|
|
|
* particular case.
|
|
|
|
****************************************************************************
|
|
|
|
* Filename: ScopeManager.java
|
|
|
|
****************************************************************************
|
|
|
|
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
|
|
|
|
***************************************************************************/
|
|
|
|
|
2012-12-28 17:28:46 +01:00
|
|
|
package org.gcube.resourcemanagement.support.server.managers.scope;
|
2012-12-19 13:00:57 +01:00
|
|
|
|
2013-09-12 18:47:59 +02:00
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
|
|
|
|
2012-12-19 13:00:57 +01:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.IOException;
|
2013-09-12 18:47:59 +02:00
|
|
|
import java.io.StringReader;
|
2012-12-19 13:00:57 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2013-09-12 18:47:59 +02:00
|
|
|
|
2012-12-19 13:00:57 +01:00
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
2013-09-12 18:47:59 +02:00
|
|
|
|
|
|
|
import org.gcube.common.resources.gcore.GenericResource;
|
|
|
|
import org.gcube.common.resources.gcore.utils.XPathHelper;
|
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
|
|
import org.gcube.common.scope.impl.ScopeBean;
|
|
|
|
import org.gcube.resourcemanagement.support.server.gcube.CacheManager;
|
|
|
|
import org.gcube.resourcemanagement.support.server.gcube.ISClientRequester;
|
2012-12-28 17:28:46 +01:00
|
|
|
import org.gcube.resourcemanagement.support.server.utils.ServerConsole;
|
2013-09-12 18:47:59 +02:00
|
|
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
|
|
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
2012-12-19 13:00:57 +01:00
|
|
|
import org.w3c.dom.Document;
|
2013-09-12 18:47:59 +02:00
|
|
|
import org.w3c.dom.Node;
|
2012-12-19 13:00:57 +01:00
|
|
|
import org.w3c.dom.NodeList;
|
2013-09-12 18:47:59 +02:00
|
|
|
import org.xml.sax.InputSource;
|
2012-12-19 13:00:57 +01:00
|
|
|
import org.xml.sax.SAXException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scopes and their maps are persisted in this structure.
|
|
|
|
*
|
|
|
|
* @author Massimiliano Assante (ISTI-CNR)
|
|
|
|
* @author Daniele Strollo
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class ScopeManager {
|
2013-09-12 18:47:59 +02:00
|
|
|
private static final Map<String, ScopeBean> SCOPES = new LinkedHashMap<String, ScopeBean>();
|
2012-12-19 13:00:57 +01:00
|
|
|
private static String confFile = null;
|
|
|
|
private static final String LOG_PREFIX = "[SCOPE-MGR]";
|
|
|
|
|
2013-09-12 18:47:59 +02:00
|
|
|
public static ScopeBean getScope(final String scope) throws Exception {
|
2012-12-19 13:00:57 +01:00
|
|
|
synchronized (SCOPES) {
|
|
|
|
if (getAvailableScopes().containsKey(scope)) {
|
|
|
|
return getAvailableScopes().get(scope);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ServerConsole.warn(LOG_PREFIX, "Using DEFAULT scope manager");
|
2013-09-12 18:47:59 +02:00
|
|
|
return new ScopeBean(scope);
|
2012-12-19 13:00:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void setScopeConfigFile(final String file) {
|
|
|
|
confFile = file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Refreshes the list of scopes and associated maps.
|
|
|
|
*/
|
|
|
|
|
2013-09-12 18:47:59 +02:00
|
|
|
public static Map<String, ScopeBean> getAvailableScopes()
|
2012-12-19 13:00:57 +01:00
|
|
|
throws Exception {
|
|
|
|
if (SCOPES.size() == 0) {
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
return SCOPES;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void clear() {
|
|
|
|
SCOPES.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void update()
|
|
|
|
throws Exception {
|
|
|
|
if (confFile == null) {
|
|
|
|
throw new NullPointerException("the scope file has not been defined");
|
|
|
|
}
|
|
|
|
String scopeXML = fileToString(confFile);
|
|
|
|
ServerConsole.info(LOG_PREFIX, "Starting retrieving scopes..");
|
|
|
|
Document scopeDocument = getDocumentGivenXML(scopeXML);
|
|
|
|
NodeList voElements = scopeDocument.getElementsByTagName("vo");
|
|
|
|
for (int i = 0; i < voElements.getLength(); i++) {
|
|
|
|
NodeList voDetails = voElements.item(i).getChildNodes();
|
|
|
|
String voString = voDetails.item(5).getFirstChild().getNodeValue();
|
|
|
|
String fileName = voDetails.item(3).getFirstChild().getNodeValue();
|
|
|
|
// String voName = voDetails.item(1).getFirstChild().getNodeValue();
|
2013-09-12 18:47:59 +02:00
|
|
|
ScopeBean vo = new ScopeBean(voString);
|
2012-12-19 13:00:57 +01:00
|
|
|
|
|
|
|
try {
|
2013-09-12 18:47:59 +02:00
|
|
|
//vo.setServiceMap(loadServiceMap((VO) vo, fileName));
|
2012-12-19 13:00:57 +01:00
|
|
|
SCOPES.put(vo.toString(), vo);
|
2013-09-12 18:47:59 +02:00
|
|
|
|
2012-12-19 13:00:57 +01:00
|
|
|
ServerConsole.info(LOG_PREFIX, " Scopes in VO " + vo.toString());
|
2013-09-12 18:47:59 +02:00
|
|
|
|
2012-12-19 13:00:57 +01:00
|
|
|
try {
|
2013-09-12 18:47:59 +02:00
|
|
|
for (String vre : getVREFromVO(vo)) {
|
2012-12-19 13:00:57 +01:00
|
|
|
// This operation overrides the vo map
|
2013-09-12 18:47:59 +02:00
|
|
|
SCOPES.put(vre.toString(), new ScopeBean(vo.toString()+"/"+vre));
|
2012-12-19 13:00:57 +01:00
|
|
|
}
|
2013-09-12 18:47:59 +02:00
|
|
|
} catch (Exception e) {
|
2012-12-19 13:00:57 +01:00
|
|
|
ServerConsole.error(LOG_PREFIX, "Exception raised while loading VREs for VO : " + vo, e);
|
|
|
|
}
|
|
|
|
}
|
2013-09-12 18:47:59 +02:00
|
|
|
catch (Exception e) {
|
2012-12-19 13:00:57 +01:00
|
|
|
ServerConsole.warn(LOG_PREFIX, "skipping... map not found for VO : " + vo, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
ServerConsole.info(LOG_PREFIX, "*** found scopes : " + SCOPES.keySet());
|
|
|
|
}
|
|
|
|
|
2013-09-12 18:47:59 +02:00
|
|
|
protected static List<String> getVREFromVO(final ScopeBean vo) throws Exception {
|
|
|
|
ServerConsole.info(LOG_PREFIX, "Starting Retrieving VREs for VO : " + vo);
|
|
|
|
List<String> toReturn = new ArrayList<String>();
|
2012-12-19 13:00:57 +01:00
|
|
|
|
2013-09-12 18:47:59 +02:00
|
|
|
ScopeProvider.instance.set(vo.toString());
|
|
|
|
SimpleQuery query = queryFor(GenericResource.class);
|
|
|
|
query.addCondition("$resource/Profile/SecondaryType/text() eq 'VRE'");
|
2012-12-19 13:00:57 +01:00
|
|
|
|
2013-09-12 18:47:59 +02:00
|
|
|
DiscoveryClient<GenericResource> client = clientFor(GenericResource.class);
|
2012-12-19 13:00:57 +01:00
|
|
|
|
2013-09-12 18:47:59 +02:00
|
|
|
List<GenericResource> gRes = client.submit(query);
|
2012-12-19 13:00:57 +01:00
|
|
|
|
2013-09-12 18:47:59 +02:00
|
|
|
for (GenericResource res : gRes) {
|
|
|
|
ServerConsole.info(LOG_PREFIX, "Found: " + res.profile().name());
|
|
|
|
toReturn.add(res.profile().name());
|
|
|
|
}
|
2012-12-19 13:00:57 +01:00
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a scope, if it is a VO or an infrastructure, the corresponding
|
|
|
|
* map file is retrieved from the globus location and the contained xml
|
|
|
|
* representation is returned.
|
|
|
|
* @param searchvo
|
|
|
|
* @return
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2013-09-12 18:47:59 +02:00
|
|
|
public static String getMapXML(final ScopeBean searchvo) throws Exception {
|
2012-12-19 13:00:57 +01:00
|
|
|
if (confFile == null) {
|
|
|
|
throw new NullPointerException("the scope file has not been defined");
|
|
|
|
}
|
|
|
|
String scopeXML = fileToString(confFile);
|
|
|
|
ServerConsole.info(LOG_PREFIX, "Starting retrieving scopes..");
|
|
|
|
Document scopeDocument = getDocumentGivenXML(scopeXML);
|
|
|
|
NodeList voElements = scopeDocument.getElementsByTagName("vo");
|
|
|
|
for (int i = 0; i < voElements.getLength(); i++) {
|
|
|
|
NodeList voDetails = voElements.item(i).getChildNodes();
|
|
|
|
String voString = voDetails.item(5).getFirstChild().getNodeValue();
|
|
|
|
String fileName = voDetails.item(3).getFirstChild().getNodeValue();
|
|
|
|
// String voName = voDetails.item(1).getFirstChild().getNodeValue();
|
2013-09-12 18:47:59 +02:00
|
|
|
ScopeBean vo = new ScopeBean(voString);
|
2012-12-19 13:00:57 +01:00
|
|
|
|
|
|
|
if (vo.equals(searchvo)) {
|
|
|
|
return fileToString(System.getenv("GLOBUS_LOCATION") + File.separator + "config" + File.separator + fileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ServerConsole.error(LOG_PREFIX, "*** maps for " + searchvo + " not found");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-09-12 18:47:59 +02:00
|
|
|
// private static ServiceMap loadServiceMap(final VO vo, final String fileName) throws Exception {
|
|
|
|
// ServiceMap map = new ServiceMap();
|
|
|
|
// String filePath = System.getenv("GLOBUS_LOCATION") + File.separator + "config" + File.separator + fileName;
|
|
|
|
// ServerConsole.info(LOG_PREFIX, "--- Loading " + vo.getName() + " from: " + filePath);
|
|
|
|
// map.load(new FileReader(filePath));
|
|
|
|
// return map;
|
|
|
|
// }
|
2012-12-19 13:00:57 +01:00
|
|
|
|
|
|
|
public static String fileToString(final String path) throws IOException {
|
|
|
|
BufferedReader filebuf = null;
|
|
|
|
String nextStr = null;
|
|
|
|
StringBuilder ret = new StringBuilder();
|
|
|
|
|
|
|
|
filebuf = new BufferedReader(new FileReader(path));
|
|
|
|
nextStr = filebuf.readLine(); // legge una riga dal file
|
|
|
|
while (nextStr != null) {
|
|
|
|
ret.append(nextStr);
|
|
|
|
nextStr = filebuf.readLine(); // legge la prossima riga
|
|
|
|
}
|
|
|
|
filebuf.close(); // chiude il file
|
|
|
|
|
|
|
|
return ret.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Document getDocumentGivenXML(final String result) {
|
|
|
|
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
|
|
|
dbf.setValidating(false);
|
|
|
|
DocumentBuilder db;
|
|
|
|
Document document = null;
|
|
|
|
try {
|
|
|
|
db = dbf.newDocumentBuilder();
|
|
|
|
document = db.parse(new ByteArrayInputStream(result.getBytes()));
|
|
|
|
} catch (ParserConfigurationException e1) {
|
|
|
|
e1.printStackTrace();
|
|
|
|
} catch (SAXException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return document;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|