001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.metasite.ejb;
034:
035: import com.hp.hpl.jena.rdf.model.Model;
036: import com.hp.hpl.jena.rdf.model.ModelFactory;
037: import com.hp.hpl.jena.rdql.Query;
038: import com.hp.hpl.jena.rdql.QueryEngine;
039: import com.hp.hpl.jena.rdql.QueryExecution;
040: import com.hp.hpl.jena.rdql.QueryResults;
041: import com.hp.hpl.jena.rdql.ResultBinding;
042:
043: import org.libresource.Libresource;
044: import org.libresource.LibresourceException;
045: import org.libresource.LibresourceResourceIdentifier;
046: import org.libresource.LibresourceResourceValue;
047: import org.libresource.LibresourceServiceBase;
048:
049: import org.libresource.core.URIResolver;
050: import org.libresource.core.interfaces.LibresourceCoreService;
051:
052: import org.libresource.kernel.KernelConstants;
053: import org.libresource.kernel.LibresourceSecurityException;
054: import org.libresource.kernel.URINotExistException;
055: import org.libresource.kernel.interfaces.KernelService;
056:
057: import org.libresource.metasite.MetaSiteConstants;
058: import org.libresource.metasite.MetaSiteServiceException;
059: import org.libresource.metasite.ejb.model.ProjectProxyResourceValue;
060: import org.libresource.metasite.ejb.model.SiteResourceValue;
061: import org.libresource.metasite.interfaces.ProjectProxyResourceLocal;
062: import org.libresource.metasite.interfaces.SiteResourceLocal;
063: import org.libresource.metasite.util.ProjectProxyResourceUtil;
064: import org.libresource.metasite.util.SiteResourceUtil;
065:
066: import org.libresource.search.LibresourceIndexableContent;
067:
068: import java.io.BufferedReader;
069: import java.io.InputStream;
070: import java.io.InputStreamReader;
071:
072: import java.net.URI;
073: import java.net.URL;
074:
075: import java.util.Date;
076: import java.util.Iterator;
077:
078: /**
079: * @libresource.service name="MetaSite" depends="Kernel,LibresourceCore"
080: */
081: public abstract class MetaSiteServiceBean extends
082: LibresourceServiceBase {
083: /**
084: * @ejb.interface-method
085: * @ejb.transaction type="Required"
086: */
087: public void createSite(URI uri, String description, String host,
088: long delay) throws MetaSiteServiceException,
089: LibresourceSecurityException {
090: try {
091: KernelService kernelService = (KernelService) Libresource
092: .getService(KernelConstants.SERVICE);
093:
094: if (!kernelService.isSuperUser()) {
095: throw new LibresourceSecurityException(uri, "Root");
096: }
097:
098: SiteResourceLocal siteResourceLocal = SiteResourceUtil
099: .getLocalHome().create(description, host, delay);
100: kernelService.bind(siteResourceLocal
101: .getLibresourceResourceIdentifier(), uri, host);
102: siteResourceLocal.batchSynchronize();
103: } catch (Exception e) {
104: ctx.setRollbackOnly();
105: throw new MetaSiteServiceException("Error in createSite : "
106: + e.getMessage(), e);
107: }
108: }
109:
110: /**
111: * @ejb.interface-method
112: * @ejb.transaction type="Required"
113: */
114: public void editSite(URI uri, String description, String host,
115: long delay) throws MetaSiteServiceException,
116: URINotExistException {
117: try {
118: KernelService kernelService = (KernelService) Libresource
119: .getService(KernelConstants.SERVICE);
120:
121: if (!kernelService.isSuperUser()) {
122: throw new LibresourceSecurityException(uri, "Root");
123: }
124:
125: LibresourceResourceIdentifier resourceIdentifier = kernelService
126: .lookup(uri);
127: SiteResourceLocal siteResourceLocal = (SiteResourceLocal) Libresource
128: .findResource(resourceIdentifier,
129: SiteResourceLocal.class);
130: siteResourceLocal.setHost(host);
131: siteResourceLocal.setDescription(description);
132: siteResourceLocal.setSynchronizationDelay(delay);
133: siteResourceLocal.batchSynchronize();
134:
135: // update node
136: kernelService.setShortName(uri, host);
137: kernelService.setUpdateDate(uri, new Date());
138:
139: // indexation
140: Libresource.index(uri);
141: } catch (URINotExistException se) {
142: ctx.setRollbackOnly();
143: throw se;
144: } catch (Exception e) {
145: ctx.setRollbackOnly();
146: throw new MetaSiteServiceException("Error in editSite : "
147: + e.getMessage(), e);
148: }
149: }
150:
151: /**
152: * @ejb.interface-method
153: * @ejb.transaction type="Supports"
154: */
155: public SiteResourceValue getSite(URI uri)
156: throws MetaSiteServiceException, URINotExistException {
157: try {
158: KernelService kernelService = (KernelService) Libresource
159: .getService(KernelConstants.SERVICE);
160: LibresourceResourceIdentifier resourceIdentifier = kernelService
161: .lookup(uri);
162: SiteResourceLocal siteResourceLocal = (SiteResourceLocal) Libresource
163: .findResource(resourceIdentifier,
164: SiteResourceLocal.class);
165: SiteResourceValue siteResourceValue = siteResourceLocal
166: .getSiteResourceValue();
167: siteResourceValue.setUri(kernelService.normalizeURI(uri));
168:
169: return siteResourceValue;
170: } catch (URINotExistException se) {
171: throw se;
172: } catch (Exception e) {
173: throw new MetaSiteServiceException("Error in getSite : "
174: + e.getMessage(), e);
175: }
176: }
177:
178: /**
179: * @ejb.interface-method
180: * @ejb.transaction type="Supports"
181: */
182: public ProjectProxyResourceValue getProjectProxy(URI uri)
183: throws MetaSiteServiceException, URINotExistException {
184: try {
185: KernelService kernelService = (KernelService) Libresource
186: .getService(KernelConstants.SERVICE);
187: LibresourceResourceIdentifier resourceIdentifier = kernelService
188: .lookup(uri);
189: ProjectProxyResourceLocal projectProxyResourceLocal = (ProjectProxyResourceLocal) Libresource
190: .findResource(resourceIdentifier,
191: ProjectProxyResourceLocal.class);
192: ProjectProxyResourceValue projectProxyResourceValue = projectProxyResourceLocal
193: .getProjectProxyResourceValue();
194: projectProxyResourceValue.setUri(kernelService
195: .normalizeURI(uri));
196:
197: return projectProxyResourceValue;
198: } catch (URINotExistException se) {
199: throw se;
200: } catch (Exception e) {
201: throw new MetaSiteServiceException(
202: "Error in getProjectProxy : " + e.getMessage(), e);
203: }
204: }
205:
206: /**
207: * @ejb.interface-method
208: * @ejb.transaction type="Supports"
209: */
210: public ProjectProxyResourceValue[] listProjectProxies(URI uri)
211: throws MetaSiteServiceException, URINotExistException {
212: try {
213: KernelService kernelService = (KernelService) Libresource
214: .getService(KernelConstants.SERVICE);
215: LibresourceResourceValue[] children = kernelService
216: .listResourcesAt(uri, MetaSiteConstants.SERVICE,
217: MetaSiteConstants.RESOURCE_PROJECT_PROXY);
218: ProjectProxyResourceValue[] projects = new ProjectProxyResourceValue[children.length];
219:
220: for (int i = 0; i < children.length; i++) {
221: LibresourceResourceIdentifier resourceIdentifier = kernelService
222: .lookup(children[i].getUri());
223: ProjectProxyResourceLocal projectProxyResourceLocal = (ProjectProxyResourceLocal) Libresource
224: .findResource(resourceIdentifier,
225: ProjectProxyResourceLocal.class);
226: ProjectProxyResourceValue projectProxyResourceValue = projectProxyResourceLocal
227: .getProjectProxyResourceValue();
228: projectProxyResourceValue.setUri(children[i].getUri());
229: projects[i] = projectProxyResourceValue;
230: }
231:
232: return projects;
233: } catch (URINotExistException se) {
234: throw se;
235: } catch (Exception e) {
236: throw new MetaSiteServiceException(
237: "Error in getProjectProxy : " + e.getMessage(), e);
238: }
239: }
240:
241: /**
242: * @ejb.interface-method
243: * @ejb.transaction type="Required"
244: */
245: public void synchronize(URI uri) throws MetaSiteServiceException,
246: URINotExistException {
247: try {
248: KernelService kernelService = (KernelService) Libresource
249: .getService(KernelConstants.SERVICE);
250:
251: if (!kernelService.checkIfOwner(uri)) {
252: throw new LibresourceSecurityException(uri, "OWNER");
253: }
254:
255: LibresourceResourceIdentifier resourceIdentifier = kernelService
256: .systemLookup(kernelService
257: .normalizeAbsoluteURIPath(uri));
258: SiteResourceLocal siteResourceLocal = (SiteResourceLocal) Libresource
259: .findResource(resourceIdentifier,
260: SiteResourceLocal.class);
261: siteResourceLocal.setLastSynchronization(new Date());
262:
263: // first pass (delete all children)
264: kernelService.systemDeleteChildren(uri);
265:
266: // retrieve public projects RDF
267: LibresourceCoreService libresourceCoreService = (LibresourceCoreService) Libresource
268: .getService("LibresourceCore");
269: URIResolver resolver = libresourceCoreService
270: .getUriResolver();
271: URL rdfUrl = new URL(resolver.resolveFeedsEndPoint(new URI(
272: siteResourceLocal.getHost()))
273: + "?publicProjects");
274: BufferedReader reader = new BufferedReader(
275: new InputStreamReader((InputStream) rdfUrl
276: .getContent()));
277:
278: // parse the RDF
279: Model model = ModelFactory.createDefaultModel();
280: model.read(reader, "");
281:
282: String queryString = "SELECT ?p, ?name, ?description, ?summary WHERE (?p <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.libresource.org/rdf/project#Project>), (?p <http://www.libresource.org/rdf/project#name> ?name), (?p <http://www.libresource.org/rdf/project#description> ?description), (?p <http://www.libresource.org/rdf/project#summary> ?summary)";
283: Query query = new Query(queryString);
284: query.setSource(model);
285:
286: QueryExecution qe = new QueryEngine(query);
287: QueryResults results = qe.exec();
288:
289: // for each remote project i create a proxy
290: for (Iterator iter = results; iter.hasNext();) {
291: ResultBinding res = (ResultBinding) iter.next();
292: String projectURI = res.get("p").toString();
293: String projectName = res.get("name").toString();
294: String projectDescription = res.get("description")
295: .toString();
296: String projectSummary = res.get("summary").toString();
297: ProjectProxyResourceLocal proxy = ProjectProxyResourceUtil
298: .getLocalHome().create(projectURI, projectName,
299: projectDescription, projectSummary);
300: URI proxyUri = new URI(uri.getPath() + "/"
301: + proxy.getId());
302: kernelService.systemCreateURI(proxyUri);
303: kernelService.systemCopyAcl(uri, proxyUri);
304: kernelService.systemBind(proxy
305: .getLibresourceResourceIdentifier(), proxyUri,
306: projectName);
307: }
308:
309: results.close();
310:
311: // update node
312: kernelService.setUpdateDate(uri, new Date());
313: } catch (URINotExistException se) {
314: ctx.setRollbackOnly();
315: throw se;
316: } catch (Exception e) {
317: ctx.setRollbackOnly();
318: throw new MetaSiteServiceException(
319: "Error in synchronize : " + e.getMessage(), e);
320: }
321: }
322:
323: // libresource service
324:
325: /**
326: * @ejb.interface-method
327: * @ejb.transaction type="Supports"
328: */
329: public LibresourceIndexableContent getIndexableContent(
330: LibresourceResourceIdentifier resourceIdentifier)
331: throws LibresourceException {
332: try {
333: try {
334: Libresource.checkType(resourceIdentifier,
335: SiteResourceLocal.class);
336:
337: LibresourceIndexableContent content = new LibresourceIndexableContent();
338: SiteResourceLocal siteResourceLocal = (SiteResourceLocal) Libresource
339: .findResource(resourceIdentifier);
340: content.addContentPart(siteResourceLocal
341: .getDescription());
342:
343: return content;
344: } catch (LibresourceException e) {
345: //
346: }
347:
348: try {
349: Libresource.checkType(resourceIdentifier,
350: ProjectProxyResourceLocal.class);
351:
352: LibresourceIndexableContent content = new LibresourceIndexableContent();
353: ProjectProxyResourceLocal projectProxyResourceLocal = (ProjectProxyResourceLocal) Libresource
354: .findResource(resourceIdentifier);
355: content.addContentPart(projectProxyResourceLocal
356: .getName());
357: content.addContentPart(projectProxyResourceLocal
358: .getDescription());
359: content.addContentPart(projectProxyResourceLocal
360: .getSummary());
361:
362: return content;
363: } catch (LibresourceException e) {
364: //
365: }
366:
367: return null;
368: } catch (Exception e) {
369: throw new LibresourceException(
370: "error in MetaSite.getIndexableContent("
371: + resourceIdentifier + ")", e);
372: }
373: }
374: }
|