View Javadoc

1   /*
2    * Copyright (c) 2007, Peter Mika All rights reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    * 
7    * - Redistributions of source code must retain the above copyright notice, this
8    *   list of conditions and the following disclaimer.
9    * - Redistributions in binary form must reproduce the above copyright notice,
10   *   this list of conditions and the following disclaimer in the documentation
11   *   and/or other materials provided with the distribution. 
12   * - Neither the name of the openrdf.org nor the names of its contributors may
13   *   be used to endorse or promote products derived from this software without
14   *   specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   * 
28   */
29  package org.openrdf.elmo.scutter;
30  
31  import java.net.URL;
32  import java.util.Collection;
33  import java.util.HashSet;
34  
35  import org.openrdf.OpenRDFException;
36  import org.openrdf.elmo.ElmoManager;
37  import org.openrdf.repository.Repository;
38  import org.openrdf.repository.sail.SailRepository;
39  import org.openrdf.sail.memory.MemoryStore;
40  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42   
43  /** This factory class creates and initializes Retriever instances.  
44   * 
45   * @author Peter Mika (pmika@cs.vu.nl)
46   *
47   */
48  public class RetrieverFactory {
49  
50  	boolean focused = false;
51  	Repository target;
52  	StatementFilter filter = null;
53      Collection targetPersons = new HashSet();
54      Repository targetPersonsRep = null;
55      boolean foafOnly = false;
56    
57      protected final static Logger _logger = LoggerFactory.getLogger(RetrieverFactory.class);
58  
59  	public RetrieverFactory(Repository target, boolean focused)  {
60  		this.focused = focused;
61  		this.target = target; 
62          if (focused) {
63              //Copy the data into an in-memory repository
64              //This is to prevent from having to re-read data when data is added
65              //to the repository and cache becomes stale
66              
67              try {
68              	//Create new non-inferencing in-memory repository and add data about the target persons
69                  //Note: by doing this we speed things up, but it means we are not using any information
70              	//about the target persons that we learn during crawling
71              	Repository targetPersonsRep = new SailRepository(new MemoryStore());
72                  targetPersonsRep.initialize();
73                  
74                  try {
75                  	Util.copyRepositoryContent(target, targetPersonsRep);
76                  }
77                  catch (OpenRDFException e) { 
78                  	throw new RuntimeException("Importing data about target persons failed.");
79                  }
80                  catch (java.io.IOException e) { // handle io exception }	
81                  	throw new RuntimeException("Importing data about target persons failed.");	
82          		}
83                  
84                  if (_logger.isDebugEnabled()) {
85                      ElmoManager manager = Util.initManager(targetPersonsRep);
86                      _logger.debug("Loaded " + Util.getAllInstances(manager, org.openrdf.concepts.foaf.Person.class).size() + 
87                                    " target persons.");
88                  }                       
89              } catch (OpenRDFException e) {
90                  _logger.error(e.getMessage(), e);
91              } 
92             
93             
94          }
95  	}
96  
97  
98  	public Retriever getRetriever(final URL url, final Scutter scutter) {
99  		Retriever result = null;
100 		if (focused) {
101 			//TODO: put this back once we put FocusedRetriever back
102 			//result = new FocusedRetriever(url, target, targetPersonsRep, scutter);
103 		} else if (foafOnly){
104             result =  new FoafRetriever(url, target, scutter);          
105         } else {
106 			result =  new SimpleRetriever(url, target, scutter);          
107 		}
108 		if (filter != null) {
109 			result.setFilter(filter);
110 		}
111 		return result;
112 	}
113 	/**
114 	 * @return Returns the focused.
115 	 */
116 	public boolean isFocused() {
117 		return focused;
118 	}
119 
120 	/**
121 	 * @param focused The focused to set.
122 	 */
123 	public void setFocused(boolean focused) {
124 		this.focused = focused;
125 	}
126 
127 	/** 
128 	 * @return Returns the filter.
129 	 */
130 	public StatementFilter getFilter() {
131 		return filter;
132 	}
133 
134 	/**
135      * Set the filter used for determining which statements to add to the
136      * repository
137      * 
138      * @param filter Filter to be used
139      */
140 	public void setFilter(StatementFilter filter) {
141 		this.filter = filter;
142 	}
143 
144 
145 	/**
146 	 * @return Returns the target.
147 	 */
148 	public Repository getTarget() {
149 		return target;
150 	}
151 
152 
153 	/**
154 	 * @param target The target to set.
155 	 */
156 	public void setTarget(Repository target) {
157 		this.target = target;
158 	}
159 
160 
161     /**
162      * @return Returns the foafOnly.
163      */
164     public boolean isFoafOnly() {
165         return foafOnly;
166     }
167 
168 
169     /**
170      * @param foafOnly The foafOnly to set.
171      */
172     public void setFoafOnly(boolean foafOnly) {
173         this.foafOnly = foafOnly;
174     }
175 	
176 	
177 
178 }