From 69da352dd6e1b6b4f6a5154f895438b3ee5a05b5 Mon Sep 17 00:00:00 2001 From: "fabio.sinibaldi" Date: Tue, 5 Mar 2013 17:00:03 +0000 Subject: [PATCH] git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/spatial-data/gis-interface@70886 82a268e6-3cf1-43bd-a215-b396298e98cf --- .classpath | 36 ++++ .project | 23 +++ .settings/org.eclipse.core.resources.prefs | 6 + .settings/org.eclipse.jdt.core.prefs | 5 + .settings/org.eclipse.m2e.core.prefs | 4 + pom.xml | 88 ++++++++++ .../gcube/spatial/data/gis/GISInterface.java | 164 ++++++++++++++++++ .../spatial/data/gis/ResearchMethod.java | 6 + .../data/gis/model/GeoServerDescriptor.java | 144 +++++++++++++++ .../data/gis/model/InfrastructureCrawler.java | 38 ++++ 10 files changed, 514 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 .settings/org.eclipse.m2e.core.prefs create mode 100644 pom.xml create mode 100644 src/main/java/org/gcube/spatial/data/gis/GISInterface.java create mode 100644 src/main/java/org/gcube/spatial/data/gis/ResearchMethod.java create mode 100644 src/main/java/org/gcube/spatial/data/gis/model/GeoServerDescriptor.java create mode 100644 src/main/java/org/gcube/spatial/data/gis/model/InfrastructureCrawler.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..534b5e5 --- /dev/null +++ b/.classpath @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..f29fd69 --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + gis-interface + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..29abf99 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,6 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/main/resources=UTF-8 +encoding//src/test/java=UTF-8 +encoding//src/test/resources=UTF-8 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..60105c1 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..ff7698f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=false +version=1 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..3c38a56 --- /dev/null +++ b/pom.xml @@ -0,0 +1,88 @@ + + 4.0.0 + + maven-parent + org.gcube.tools + 1.0.0 + + org.gcube.spatial.data + gis-interface + 1.0.0-SNAPSHOT + gis-interface + + + org.gcube.spatial.data + geonetwork + [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT] + + + it.geosolutions + geoserver-manager + 1.5-SNAPSHOT + + + org.gcube.resources.discovery + ic-client + [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT] + + + + + + + maven-compiler-plugin + + 1.6 + 1.6 + + + + + org.apache.maven.plugins + maven-resources-plugin + 2.5 + + + copy-profile + install + + copy-resources + + + target + + + ${distroDirectory} + true + + profile.xml + + + + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + ${distroDirectory}/descriptor.xml + + + + + servicearchive + install + + single + + + + + + + + \ No newline at end of file diff --git a/src/main/java/org/gcube/spatial/data/gis/GISInterface.java b/src/main/java/org/gcube/spatial/data/gis/GISInterface.java new file mode 100644 index 0000000..82fbc42 --- /dev/null +++ b/src/main/java/org/gcube/spatial/data/gis/GISInterface.java @@ -0,0 +1,164 @@ +package org.gcube.spatial.data.gis; + +import it.geosolutions.geoserver.rest.GeoServerRESTManager; +import it.geosolutions.geoserver.rest.GeoServerRESTPublisher; +import it.geosolutions.geoserver.rest.GeoServerRESTReader; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.SortedSet; +import java.util.concurrent.ConcurrentSkipListSet; + +import org.gcube.spatial.data.geonetwork.GeoNetwork; +import org.gcube.spatial.data.geonetwork.GeoNetworkPublisher; +import org.gcube.spatial.data.geonetwork.GeoNetworkReader; +import org.gcube.spatial.data.gis.model.GeoServerDescriptor; +import org.gcube.spatial.data.gis.model.InfrastructureCrawler; + +public class GISInterface { + + private static final long MAX_GEOSERVER_CACHE_TIME=2*60*1000; + + public static GISInterface get() throws Exception{ + return new GISInterface(); + } + + + //************ INSTANCE + + + + + private GISInterface() throws Exception{ + + } + + //***************** GEONETWORK READ METHODS + +// public GNSearchResponse query(GNSearchRequest request) +// throws GNLibException, GNServerException, Exception{ +// return getGN().query(request); +// } +// +// public GNSearchResponse query(File request) +// throws GNLibException, GNServerException, Exception{ +// return getGN().query(request); +// } +// +// public Metadata getById(long id) throws GNLibException, +// GNServerException, JAXBException, Exception { +// return getGN().getById(id); +// } +// +// public Metadata getById(String UUID) throws GNLibException, +// GNServerException, JAXBException, Exception { +// return getGN().getById(UUID); +// } + + //****************** GEONETWORK WRITE METHODS + +// public void deleteMetadata(long id) throws GNLibException, +// GNServerException, AuthorizationException, Exception { +// getGN().deleteMetadata(id); +// } +// +// public void setPrivileges(long metadataId, GNPrivConfiguration cfg) +// throws GNLibException, GNServerException, AuthorizationException,Exception{ +// getGN().setPrivileges(metadataId, cfg); +// } +// +// public long insertMetadata(GNInsertConfiguration configuration, +// File metadataFile) throws GNLibException, GNServerException, +// AuthorizationException,Exception{ +// return getGN().insertMetadata(configuration, metadataFile); +// } +// +// public long insertMetadata(GNInsertConfiguration configuration, +// Metadata meta) throws GNLibException, GNServerException, +// AuthorizationException, IOException, JAXBException,Exception{ +// return getGN().insertMetadata(configuration, meta); +// } +// +// public long insertMetadata(File requestFile) throws GNLibException, +// GNServerException, AuthorizationException,Exception{ +// return getGN().insertMetadata(requestFile); +// } +// +// public long insertMetadata(Metadata meta) throws GNLibException, +// GNServerException, AuthorizationException, IOException, +// JAXBException,Exception{ +// return getGN().insertMetadata(meta); +// } +// +// public void updateMetadata(long id, File metadataFile) +// throws GNLibException, GNServerException, AuthorizationException,Exception{ +// getGN().updateMetadata(id, metadataFile); +// } +// +// public void updateMetadata(long id, Metadata meta) throws GNLibException, +// GNServerException, AuthorizationException, IOException, +// JAXBException,Exception{ +// getGN().updateMetadata(id, meta); +// } + + //*******************READER getter METHODS + + public GeoNetworkReader getGeoNetworkReader() throws Exception{ + return getGN(); + } + + public GeoNetworkPublisher getGeoNewtorkPublisher()throws Exception{ + return getGN(); + } + + public GeoServerRESTReader getGeoServerReader(ResearchMethod method,boolean forceRefresh) throws Exception{ + GeoServerDescriptor desc=getGeoServerSet(forceRefresh).last(); + return getGeoServerManager(desc.getUrl(), desc.getUser(), desc.getPassword()).getReader(); + } + + public GeoServerRESTReader getGeoServerReader(String url,String user,String password) throws IllegalArgumentException, MalformedURLException{ + return getGeoServerManager(url, user, password).getReader(); + } + + public GeoServerRESTReader getGeoServerReader(String url) throws MalformedURLException{ + return new GeoServerRESTReader(url); + } + + //******************* Complex logic + + public void addGeoTIFF() throws Exception{ + GeoServerDescriptor desc=getGeoServerSet(false).last(); + GeoServerRESTPublisher publisher=getGeoServerManager(desc.getUrl(), desc.getUser(), desc.getPassword()).getPublisher(); + publisher. + } + + + + + //************ PRIVATE + + private GeoNetworkPublisher geoNetwork=null; + private ConcurrentSkipListSet geoservers=new ConcurrentSkipListSet(); + private long lastAccessedTime=0l; + + private synchronized SortedSet getGeoServerSet(boolean forceRefresh) throws Exception{ + if(forceRefresh||geoservers.size()==0||System.currentTimeMillis()-lastAccessedTime>MAX_GEOSERVER_CACHE_TIME){ + geoservers.clear(); + geoservers.addAll(InfrastructureCrawler.queryforGeoServer()); + lastAccessedTime=System.currentTimeMillis(); + } + return geoservers; + } + + private synchronized GeoNetworkPublisher getGN() throws Exception{ + if(geoNetwork==null) geoNetwork=GeoNetwork.get(); + return geoNetwork; + } + + private GeoServerRESTManager getGeoServerManager(String url,String user,String password) throws IllegalArgumentException, MalformedURLException{ + return new GeoServerRESTManager(new URL(url), user, password); + } + + + +} diff --git a/src/main/java/org/gcube/spatial/data/gis/ResearchMethod.java b/src/main/java/org/gcube/spatial/data/gis/ResearchMethod.java new file mode 100644 index 0000000..8449778 --- /dev/null +++ b/src/main/java/org/gcube/spatial/data/gis/ResearchMethod.java @@ -0,0 +1,6 @@ +package org.gcube.spatial.data.gis; + +public enum ResearchMethod { + + RANDOM,MOSTUNLOAD +} diff --git a/src/main/java/org/gcube/spatial/data/gis/model/GeoServerDescriptor.java b/src/main/java/org/gcube/spatial/data/gis/model/GeoServerDescriptor.java new file mode 100644 index 0000000..f74b4ca --- /dev/null +++ b/src/main/java/org/gcube/spatial/data/gis/model/GeoServerDescriptor.java @@ -0,0 +1,144 @@ +package org.gcube.spatial.data.gis.model; + +public class GeoServerDescriptor implements Comparable{ + + + private String url; + private String user; + private String password; + private Long hostedLayersCount; + + + public GeoServerDescriptor(String url, String user, String password, + Long hostedLayersCount) { + super(); + this.url = url; + this.user = user; + this.password = password; + this.hostedLayersCount = hostedLayersCount; + } + + + /* (non-Javadoc) + * @see java.lang.Comparable#compareTo(java.lang.Object) + */ + @Override + public int compareTo(GeoServerDescriptor o) { + // TODO Auto-generated method stub + return hostedLayersCount.compareTo(o.hostedLayersCount); + } + + /** + * @return the url + */ + public String getUrl() { + return url; + } + + /** + * @param url the url to set + */ + public void setUrl(String url) { + this.url = url; + } + + /** + * @return the user + */ + public String getUser() { + return user; + } + + /** + * @param user the user to set + */ + public void setUser(String user) { + this.user = user; + } + + /** + * @return the password + */ + public String getPassword() { + return password; + } + + /** + * @param password the password to set + */ + public void setPassword(String password) { + this.password = password; + } + + /** + * @return the hostedLayersCount + */ + public Long getHostedLayersCount() { + return hostedLayersCount; + } + + /** + * @param hostedLayersCount the hostedLayersCount to set + */ + public void setHostedLayersCount(Long hostedLayersCount) { + this.hostedLayersCount = hostedLayersCount; + } + + + /* (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((url == null) ? 0 : url.hashCode()); + return result; + } + + + /* (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + GeoServerDescriptor other = (GeoServerDescriptor) obj; + if (url == null) { + if (other.url != null) + return false; + } else if (!url.equals(other.url)) + return false; + return true; + } + + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("GeoServerDescriptor [url="); + builder.append(url); + builder.append(", user="); + builder.append(user); + builder.append(", password="); + builder.append(password); + builder.append(", hostedLayersCount="); + builder.append(hostedLayersCount); + builder.append("]"); + return builder.toString(); + } + + + + + + +} diff --git a/src/main/java/org/gcube/spatial/data/gis/model/InfrastructureCrawler.java b/src/main/java/org/gcube/spatial/data/gis/model/InfrastructureCrawler.java new file mode 100644 index 0000000..d00299e --- /dev/null +++ b/src/main/java/org/gcube/spatial/data/gis/model/InfrastructureCrawler.java @@ -0,0 +1,38 @@ +package org.gcube.spatial.data.gis.model; + +import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; +import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; + +import java.util.ArrayList; +import java.util.List; + +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; + +public class InfrastructureCrawler { + + + public static List queryforGeoServer() throws Exception{ + List toReturn=new ArrayList(); + SimpleQuery query = queryFor(ServiceEndpoint.class); + + query.addCondition("$resource/Profile/Category/text() eq 'Gis'") + .addCondition("$resource/Profile/Platform/Name/text() eq 'geonetwork'") + .setResult("$resource/Profile/AccessPoint"); + + DiscoveryClient client = clientFor(AccessPoint.class); + + List accesspoints = client.submit(query); +// if(accesspoints.size()==0) throw new Exception("No Resource found under current scope "+ScopeProvider.instance.get()); + for (AccessPoint point : accesspoints) { + toReturn.add(new GeoServerDescriptor(point.address(),point.username(),point.password(),0l)); +// url=point.address(); +// user=point.username(); +// pwd=point.password(); + } + return toReturn; + } + +}