|
From: <li...@us...> - 2008-10-31 01:40:53
|
Revision: 2252
http://cogkit.svn.sourceforge.net/cogkit/?rev=2252&view=rev
Author: liuwt
Date: 2008-10-31 01:35:51 +0000 (Fri, 31 Oct 2008)
Log Message:
-----------
Modified Paths:
--------------
trunk/current/src/cog/modules/transfer-gui/src/org/globus/transfer/reliable/client/eprmgmt/EPRManager.java
Modified: trunk/current/src/cog/modules/transfer-gui/src/org/globus/transfer/reliable/client/eprmgmt/EPRManager.java
===================================================================
--- trunk/current/src/cog/modules/transfer-gui/src/org/globus/transfer/reliable/client/eprmgmt/EPRManager.java 2008-10-31 01:35:35 UTC (rev 2251)
+++ trunk/current/src/cog/modules/transfer-gui/src/org/globus/transfer/reliable/client/eprmgmt/EPRManager.java 2008-10-31 01:35:51 UTC (rev 2252)
@@ -1,10 +1,14 @@
package org.globus.transfer.reliable.client.eprmgmt;
import java.io.File;
+import java.io.FileFilter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import java.io.Writer;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
import javax.xml.namespace.QName;
@@ -15,11 +19,112 @@
import org.globus.wsrf.encoding.ObjectSerializer;
import org.xml.sax.InputSource;
-public class EPRManager {
- private final static String EPR_DIR = "epr";
+public class EPRManager {
+ private final static String ERP_FILE_SUFFIX = ".epr";
+ private List<EndpointReferenceType> eprList;
private static Log logger = LogFactory.getLog(EPRManager.class);
- public void saveEPR(EndpointReferenceType epr) throws Exception {
+ public EPRManager() {
+ eprList = new ArrayList<EndpointReferenceType>();
+ }
+
+ public boolean deleteEPRFile(String location, String resourceKey) throws IllegalArgumentException {
+ if (null == location) {
+ throw new IllegalArgumentException("EPR location can not be null");
+ }
+
+ if (null == resourceKey || "".equals(resourceKey.trim())) {
+ throw new IllegalArgumentException("resource key can not be null");
+ }
+
+ String eprFileName = resourceKey + ERP_FILE_SUFFIX;
+ File file = new File(location, eprFileName);
+ return file.delete();
+ }
+
+ public void addEPR (EndpointReferenceType epr) {
+ eprList.add(epr);
+ }
+
+ public void removeEPR (EndpointReferenceType epr) {
+ eprList.remove(epr);
+ }
+
+
+ public int getNumEPR () {
+ return eprList.size();
+ }
+
+ public EndpointReferenceType getEPR (int index) throws IllegalArgumentException {
+ if (index < 0 || index >= eprList.size()) {
+ throw new IllegalArgumentException("index is invalid");
+ }
+
+ return eprList.get(index);
+ }
+
+ public EndpointReferenceType getEPR (String resourceKey) throws IllegalArgumentException{
+ if (null == resourceKey) {
+ throw new IllegalArgumentException("resource key can not be null");
+ }
+
+ Iterator<EndpointReferenceType> iter = eprList.iterator();
+ EndpointReferenceType ret = null;
+ while (iter.hasNext()) {
+ ret = iter.next();
+ if (resourceKey.equals(ret.getProperties().get_any()[0].getValue())) {
+ return ret;
+ }
+ }
+
+ return null;
+ }
+
+ public String getResourceKey (int index) throws IllegalArgumentException {
+ if (index < 0 || index >= eprList.size()) {
+ throw new IllegalArgumentException("index is invalid");
+ }
+
+ EndpointReferenceType epr = eprList.get(index);
+ return epr.getProperties().get_any()[0].getValue();
+ }
+
+ public List<EndpointReferenceType> getEPRList () {
+ return eprList;
+ }
+
+ public void writeEPRList (String location) throws Exception{
+ if (null == location) {
+ throw new IllegalArgumentException("EPR location can not be null");
+ }
+
+ File eprDir = new File(location);
+ if (!eprDir.exists() || !eprDir.isDirectory()) {
+ eprDir.mkdirs();
+ }
+
+ Iterator<EndpointReferenceType> iter = eprList.iterator();
+ while (iter.hasNext()) {
+ saveEPR(iter.next(), eprDir);
+ }
+
+ }
+
+ public void readEPRList (String location) throws Exception {
+ if (null == location) {
+ throw new IllegalArgumentException("EPR location can not be null");
+ }
+
+ File eprDir = new File(location);
+ if (!eprDir.exists() || !eprDir.isDirectory()) {
+ throw new Exception("the directory for storing EPR does not exist, read operation failed");
+ }
+
+ List<EndpointReferenceType> savedEPRs = readAll(eprDir);
+ eprList.addAll(savedEPRs);
+ }
+
+ private void saveEPR(EndpointReferenceType epr, File eprDir) throws Exception {
if (null == epr) {
throw new Exception("can not save a null EPR");
}
@@ -30,7 +135,8 @@
}
QName qname = new QName("", "epr");
- File file = new File(EPR_DIR, resourceKey);
+ String eprFileName = resourceKey + ERP_FILE_SUFFIX;
+ File file = new File(eprDir, eprFileName);
Writer writer = null;
try {
writer = new FileWriter(file);
@@ -46,54 +152,38 @@
}
- public EndpointReferenceType getEPR(String resourceKey) throws Exception {
- if (null == resourceKey || "".equals(resourceKey.trim())) {
- throw new Exception("resource key can not be null");
- }
+ private List<EndpointReferenceType> readAll(File eprDir) throws Exception {
+ List<EndpointReferenceType> ret = new ArrayList<EndpointReferenceType>();
+ File[] eprFiles = eprDir.listFiles(new FileFilter() {
+ public boolean accept(File file) {
+ if(file.isFile() && file.getName().endsWith(ERP_FILE_SUFFIX)) {
+ return true;
+ }
+
+ return false;
+ }
+ });
- File file = new File(EPR_DIR, resourceKey);
- if (!file.exists() || !file.isFile()) {
- return null;
- }
-
- Reader reader = null;
- try {
- reader = new FileReader(file);
- InputSource inputSource = new InputSource(reader);
- EndpointReferenceType ret = (EndpointReferenceType)ObjectDeserializer.deserialize(
- inputSource, EndpointReferenceType.class);
-
- return ret;
- } catch(Exception e) {
- logger.debug(e.getMessage(), e);
- } finally {
- if (null != reader) {
- reader.close();
+ if (null != eprFiles) {
+ Reader reader = null;
+ for (int i = 0; i < eprFiles.length; i++) {
+ try {
+ reader = new FileReader(eprFiles[i]);
+ InputSource inputSource = new InputSource(reader);
+ EndpointReferenceType epr = (EndpointReferenceType)ObjectDeserializer.deserialize(
+ inputSource, EndpointReferenceType.class);
+ ret.add(epr);
+ } catch (Exception e) {
+ logger.debug(e.getMessage(), e);
+ } finally {
+ if (null != reader) {
+ reader.close();
+ reader = null;
+ }
+ }
}
}
- return null;
+ return ret;
}
-
- public boolean deleteEPR(String resourceKey) {
- if (null == resourceKey || "".equals(resourceKey.trim())) {
- return true;
- }
-
- File file = new File(EPR_DIR, resourceKey);
- return file.delete();
- }
-
- public static void main(String[] args) {
- EPRManager e = new EPRManager();
- EndpointReferenceType p;
- try {
- p = e.getEPR("22558696");
- //System.out.println(p);
- } catch (Exception e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
-
- }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|