001/*
002 * Copyright (c) 2003-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2012-11-26 22:27:07 +0000 (Mon, 26 Nov 2012) $' 
007 * '$Revision: 31133 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.objectmanager.repository;
031
032import java.io.InputStream;
033import java.util.Iterator;
034import java.util.List;
035
036import org.apache.commons.logging.Log;
037import org.apache.commons.logging.LogFactory;
038import org.kepler.authentication.AuthenticationException;
039import org.kepler.configuration.ConfigurationManager;
040import org.kepler.configuration.ConfigurationProperty;
041import org.kepler.objectmanager.lsid.KeplerLSID;
042
043/**
044 * This interface represents a repository on the ecogrid
045 * 
046 * @author Chad Berkley
047 */
048public abstract class Repository {
049        private static final Log log = LogFactory.getLog(Repository.class.getName());
050        private static final boolean isDebugging = log.isDebugEnabled();
051        
052        protected String name;
053        protected String repository;
054        protected String putPath;
055        protected String authDomain;
056        protected String queryPath;
057        protected String authenticatedQueryPath;
058        protected String authorizationPath;
059        protected String lsidPath;
060        protected String registry;
061        protected String registryauth;
062        protected String authProtocol;
063        protected String lsidAuthority;
064        
065        protected boolean includeInSearch;
066        protected boolean includeInSave;
067
068        /**
069         * constructor
070         * 
071         * @param repository
072         *            the address of the repository
073         * @param username
074         *            the username of the repository
075         * @param password
076         *            the password for the repository
077         * @param authProtocol
078         *            protocol (http or https for the repository)
079         */
080        public Repository(String name, String repository, String putPath,
081                        String authDomain, String lsidPath, String queryPath, String authenticatedQueryPath, 
082                        String authorizationPath, String registry, String registryauth, String authProtocol, 
083                        String lsidAuthority) {
084                this.name = name;
085                this.repository = repository;
086                this.putPath = putPath;
087                this.authDomain = authDomain;
088                this.queryPath = queryPath;
089                this.authenticatedQueryPath = authenticatedQueryPath;
090                this.authorizationPath = authorizationPath;
091                this.lsidPath = lsidPath;
092                this.registry = registry;
093                this.registryauth = registryauth;
094                this.authProtocol = authProtocol;
095                this.lsidAuthority = lsidAuthority;
096                
097                this.includeInSearch = false;
098                this.includeInSave = false;
099        }
100
101        /**
102         * Search the repository and return an iterator of results (the Iterator
103         * contains KeplerLSIDs of matched items).
104         * @throws AuthenticationException 
105         */
106        public abstract Iterator search(String queryString, boolean authenticate)
107                        throws RepositoryException, AuthenticationException;
108
109        /**
110         * return the object from the repository that has the given lsid
111         * @throws AuthenticationException 
112         */
113        public abstract InputStream get(KeplerLSID lsid, boolean authenticate) throws RepositoryException, AuthenticationException;
114
115        /**
116         * get method with a string id
117         * @throws AuthenticationException 
118         */
119        public abstract InputStream get(String id, boolean authenticate) throws RepositoryException, AuthenticationException;
120
121        /**
122         * put an object into the repository with a predetermined sessionid
123         */
124        public abstract void put(Object o, KeplerLSID lsid, String sessionid)
125                        throws RepositoryException;
126
127        /**
128         * get the url for the lsid service associated with this repository
129         */
130        public abstract String getLSIDServerURL();
131
132        /**
133         * get the repository url
134         */
135        public String getRepository() {
136                return repository;
137        }
138
139        /**
140         * get the name of the repository
141         */
142        public String getName() {
143                return name;
144        }
145        
146        /**
147         * get the registry of the repository
148         */
149        public String getRegistry() {
150                return registry;
151        }
152
153        /**
154         * return the authentication service path
155         */
156        public String getAuthDomain() {
157                return authDomain;
158        }
159
160        /**
161         * return the put service path
162         */
163        public String getPutPath() {
164                return putPath;
165        }
166
167        /**
168         * return the query service path
169         */
170        public String getQueryPath() {
171                return queryPath;
172        }
173        
174        /**
175         * return the authenticated query service path
176         */
177        public String getAuthenticatedQueryPath() {
178                return authenticatedQueryPath;
179        }
180
181        /**
182         * return the lsid service path
183         */
184        public String getLSIDPath() {
185                return lsidPath;
186        }
187        
188        /**
189         * 
190         * return the authorization service path
191         */
192        public String getAuthorizationPath(){
193          return authorizationPath;
194        }
195
196        /**
197         * return the protocol of the authentication
198         */
199        public String getAuthProtocol() {
200                return authProtocol;
201        }
202
203        public String getLsidAuthority() {
204                return lsidAuthority;
205        }
206
207        public boolean includeInSearch() {
208                return includeInSearch;
209        }
210
211        public void setIncludeInSearch(boolean includeInSearch) {
212                this.includeInSearch = includeInSearch;
213                
214                //write the value to the config file
215                String val = "false";
216                if(includeInSearch)
217                {
218                  val = "true";
219                }
220                                
221                try
222                {
223      ConfigurationProperty prop = ConfigurationManager.getInstance()
224          .getProperty(ConfigurationManager.getModule("repository"));
225      List reposList = prop.getProperties("repository");
226      for (int i = 0; i < reposList.size(); i++) 
227      {
228        ConfigurationProperty cp = (ConfigurationProperty) reposList.get(i);
229        if(!cp.getProperty("name").getValue().equals(this.name))
230        {
231          continue;
232        }
233        
234        ConfigurationProperty srProp = cp.getProperty("searchRepository");
235        if(srProp != null)
236        {
237          srProp.setValue(val);
238        }
239        else
240        {
241          srProp = new ConfigurationProperty(cp.getModule(), "searchRepository");
242          srProp.setValue(val);
243          cp.addProperty(srProp);
244        }
245      }
246      ConfigurationManager.getInstance().saveConfiguration();
247    }
248    catch(Exception e)
249    {
250      System.out.println("Error: could not write search repository to the config file: " + e.getMessage());
251      e.printStackTrace();
252    }
253        }
254        
255        /**
256         * If the repository is included in saving process
257         * @return true if it is included in saving process
258         */
259        public boolean includeInSave() {
260    return includeInSave;
261  }
262
263        /**
264         * Set the repository to be included in saving process or not
265         * @param includeInSave true if the repository should be included
266         */
267  public void setIncludeInSave(boolean includeInSave) {
268    this.includeInSave = includeInSave;
269  }
270
271        /**
272         * return a string rep of this object for debug purposes
273         */
274        public String toString() {
275                String s = "name=" + name + ", repository=" + repository;
276                return s;
277                
278        }
279}