Author: bfoster
Date: Mon Dec 13 17:24:08 2010
New Revision: 1045240

URL: http://svn.apache.org/viewvc?rev=1045240&view=rev
Log:

- create Cached JobRepository for cas-resource using XStream

--------------------

OODT-80

Added:
    
oodt/trunk/resource/src/main/java/org/apache/oodt/cas/resource/jobrepo/XStreamJobRepository.java
   (with props)
    
oodt/trunk/resource/src/main/java/org/apache/oodt/cas/resource/jobrepo/XStreamJobRepositoryFactory.java
   (with props)
Modified:
    oodt/trunk/CHANGES.txt
    oodt/trunk/resource/pom.xml

Modified: oodt/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/oodt/trunk/CHANGES.txt?rev=1045240&r1=1045239&r2=1045240&view=diff
==============================================================================
--- oodt/trunk/CHANGES.txt (original)
+++ oodt/trunk/CHANGES.txt Mon Dec 13 17:24:08 2010
@@ -4,6 +4,8 @@ Apache OODT Change Log
 Release 0.2 (Current Development)
 --------------------------------------------
 
+* OODT-80 Create Cached JobRepository for cas-resource (bfoster)
+
 * OODT-82 Make resource manager's node ip addresses envReplace-able (bfoster)
 
 * OODT-83 the artifactid for pushpull should change to cas-pushpull (Faranak 
Davoodi via mattmann)

Modified: oodt/trunk/resource/pom.xml
URL: 
http://svn.apache.org/viewvc/oodt/trunk/resource/pom.xml?rev=1045240&r1=1045239&r2=1045240&view=diff
==============================================================================
--- oodt/trunk/resource/pom.xml (original)
+++ oodt/trunk/resource/pom.xml Mon Dec 13 17:24:08 2010
@@ -139,6 +139,18 @@ the License.
       <version>2.0.1</version>
     </dependency>
     <dependency>
+      <groupId>com.thoughtworks.xstream</groupId>
+      <artifactId>xstream</artifactId>
+      <version>1.3.1</version>
+      <exclusions>
+        <exclusion>
+          <!-- xom is an optional dependency of xstream. Its also an Apache 
incompatible license -->
+          <groupId>xom</groupId>
+          <artifactId>xom</artifactId>
+        </exclusion> 
+      </exclusions>
+    </dependency>
+    <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>3.8.2</version>

Added: 
oodt/trunk/resource/src/main/java/org/apache/oodt/cas/resource/jobrepo/XStreamJobRepository.java
URL: 
http://svn.apache.org/viewvc/oodt/trunk/resource/src/main/java/org/apache/oodt/cas/resource/jobrepo/XStreamJobRepository.java?rev=1045240&view=auto
==============================================================================
--- 
oodt/trunk/resource/src/main/java/org/apache/oodt/cas/resource/jobrepo/XStreamJobRepository.java
 (added)
+++ 
oodt/trunk/resource/src/main/java/org/apache/oodt/cas/resource/jobrepo/XStreamJobRepository.java
 Mon Dec 13 17:24:08 2010
@@ -0,0 +1,143 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.oodt.cas.resource.jobrepo;
+
+//JDK imports
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import java.util.Vector;
+
+//CommonsIO imports
+import org.apache.commons.io.FileUtils;
+
+//XStream imports
+import com.thoughtworks.xstream.XStream;
+
+//OODT imports
+import org.apache.oodt.cas.resource.structs.JobSpec;
+import org.apache.oodt.cas.resource.structs.JobStatus;
+import org.apache.oodt.cas.resource.structs.exceptions.JobRepositoryException;
+
+/**
+ * @author bfoster
+ * @version $Revision$
+ * 
+ * XStream based JobRepository
+ */
+public class XStreamJobRepository implements JobRepository {
+
+       private File workingDir;
+       private final int maxHistory;
+       private Map<String, String> jobMap;
+       private List<String> jobPrecedence;
+       
+       public XStreamJobRepository(File workingDir, int maxHistory) {
+               this.workingDir = workingDir;
+               this.maxHistory = Math.max(maxHistory == -1 ? Integer.MAX_VALUE 
: maxHistory, 1);
+               this.jobMap = Collections.synchronizedMap(new HashMap<String, 
String>());
+               this.jobPrecedence = new Vector<String>();
+       }
+       
+       public synchronized String addJob(JobSpec spec) throws 
JobRepositoryException {
+           XStream xstream = new XStream();
+           FileOutputStream os = null;
+               try {
+                       if (this.jobMap.size() >= this.maxHistory)
+                               FileUtils.forceDelete(new 
File(jobMap.remove(jobPrecedence.remove(0))));
+                       
+                       if (spec.getJob().getId() == null)
+                           spec.getJob().setId(UUID.randomUUID().toString());
+                       else if (this.jobMap.containsKey(spec.getJob().getId()))
+                               throw new JobRepositoryException("JobId '" + 
spec.getJob().getId() + "' already in use -- must pick unique JobId");
+                       
+                       File file = 
this.generateFilePath(spec.getJob().getId());
+                       os = new FileOutputStream(file);
+                   xstream.toXML(spec, os);
+                   jobMap.put(spec.getJob().getId(), file.getAbsolutePath());
+                   jobPrecedence.add(spec.getJob().getId());
+                       return spec.getJob().getId();
+               }catch (Exception e) {
+                       throw new JobRepositoryException("Failed to add job 
spec to repo : " + e.getMessage(), e);
+               }finally {
+                       try {
+                               os.close();
+                       }catch (Exception e) {}
+               }
+       }
+
+       public JobSpec getJobById(String jobId) throws JobRepositoryException {
+           XStream xstream = new XStream();
+           FileInputStream is = null;
+               try {
+                       is = new FileInputStream(new 
File(this.jobMap.get(jobId)));
+                   return (JobSpec) xstream.fromXML(is);
+               }catch (Exception e) {
+                       throw new JobRepositoryException("Failed to load job 
spec from repo by id '" + jobId + "' : " + e.getMessage(), e);
+               }finally {
+                       try {
+                               is.close();
+                       }catch (Exception e) {}
+               }
+       }
+
+       public String getStatus(JobSpec spec) throws JobRepositoryException {
+               return 
this.getJobById(spec.getJob().getId()).getJob().getStatus();
+       }
+
+       public boolean jobFinished(JobSpec spec) throws JobRepositoryException {
+               String status = this.getStatus(spec);
+           return status.equals(JobStatus.COMPLETE);
+       }
+
+       public synchronized void removeJob(JobSpec spec) throws 
JobRepositoryException {
+               try {
+                       FileUtils.forceDelete(new 
File(this.jobMap.get(spec.getJob().getId())));
+                   jobMap.remove(spec.getJob().getId());
+                   jobPrecedence.remove(spec.getJob().getId());
+               }catch (Exception e) {
+                       throw new JobRepositoryException("Failed to delete job 
'" + spec.getJob().getId() + "' : " + e.getMessage(), e);
+               }
+       }
+
+       public synchronized void updateJob(JobSpec spec) throws 
JobRepositoryException {
+           XStream xstream = new XStream();
+           FileOutputStream os = null;
+               try {
+                       FileUtils.forceDelete(new 
File(this.jobMap.get(spec.getJob().getId())));
+                       os = new FileOutputStream(new 
File(this.jobMap.get(spec.getJob().getId())));
+                   xstream.toXML(spec, os);
+               }catch (Exception e) {
+                       throw new JobRepositoryException("Failed to add job 
spec to repo : " + e.getMessage(), e);
+               }finally {
+                       try {
+                               os.close();
+                       }catch (Exception e) {}
+               }
+       }
+       
+       protected File generateFilePath(String jobId) {
+               return new File(workingDir, jobId + ".xstream");
+       }
+
+}

Propchange: 
oodt/trunk/resource/src/main/java/org/apache/oodt/cas/resource/jobrepo/XStreamJobRepository.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
oodt/trunk/resource/src/main/java/org/apache/oodt/cas/resource/jobrepo/XStreamJobRepositoryFactory.java
URL: 
http://svn.apache.org/viewvc/oodt/trunk/resource/src/main/java/org/apache/oodt/cas/resource/jobrepo/XStreamJobRepositoryFactory.java?rev=1045240&view=auto
==============================================================================
--- 
oodt/trunk/resource/src/main/java/org/apache/oodt/cas/resource/jobrepo/XStreamJobRepositoryFactory.java
 (added)
+++ 
oodt/trunk/resource/src/main/java/org/apache/oodt/cas/resource/jobrepo/XStreamJobRepositoryFactory.java
 Mon Dec 13 17:24:08 2010
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.oodt.cas.resource.jobrepo;
+
+//OODT imports
+import org.apache.oodt.cas.metadata.util.PathUtils;
+
+//JDK imports
+import java.io.File;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * @author bfoster
+ * @version $Revision$
+ * 
+ * Factory for creating XStream based JobRepository
+ */
+public class XStreamJobRepositoryFactory implements JobRepositoryFactory {
+
+       private static final Logger LOG = 
Logger.getLogger(XStreamJobRepositoryFactory.class.getName());
+       
+       public XStreamJobRepository createRepository() {
+               try {
+                       String workingDirPropVal = 
System.getProperty("org.apache.oodt.cas.resource.jobrepo.xstream.working.dir");
+                       if (workingDirPropVal == null)
+                               return null;
+                       else
+                               workingDirPropVal = 
PathUtils.doDynamicReplacement(workingDirPropVal);
+                       File working = new File(workingDirPropVal);
+                       if (!working.exists())
+                               working.mkdirs();
+                       int maxHistory = 
Integer.parseInt(System.getProperty("org.apache.oodt.cas.resource.jobrepo.xstream.max.history",
 "-1"));
+                       return new XStreamJobRepository(working, maxHistory);
+               }catch (Exception e) {
+                       LOG.log(Level.SEVERE, "Failed to loaded 
XStreamJobRepository : " + e.getMessage(), e);
+                       return null;
+               }
+       }
+
+}

Propchange: 
oodt/trunk/resource/src/main/java/org/apache/oodt/cas/resource/jobrepo/XStreamJobRepositoryFactory.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to