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;
034:
035: import java.net.URI;
036:
037: import java.text.SimpleDateFormat;
038:
039: import java.util.Date;
040:
041: import javax.jms.JMSException;
042: import javax.jms.Message;
043:
044: /**
045: * LibreSource
046: *
047: * @author <a href="mailto:bort@loria.fr">Guillaume Bort</a> - <a
048: * href="http://www.inria.fr">INRIA Lorraine</a>
049: */
050: public class LibresourceEvent {
051: private URI fromResource;
052: private String eventType;
053: private String serviceName;
054: private String resourceType;
055: private URI throwedBy;
056: private Date date;
057: private String args;
058:
059: private LibresourceEvent() {
060: }
061:
062: public LibresourceEvent(URI fromResource,
063: LibresourceResourceIdentifier identifier, URI user,
064: String eventType, String args) throws LibresourceException {
065: try {
066: if (identifier != null) {
067: this .serviceName = identifier.getService();
068: this .resourceType = identifier.getResourceType();
069: } else {
070: this .serviceName = "empty";
071: this .resourceType = "empty";
072: }
073:
074: this .throwedBy = user;
075: this .date = new Date();
076: this .eventType = eventType;
077: this .fromResource = fromResource;
078: this .args = args;
079: } catch (Exception e) {
080: throw new LibresourceException(
081: "Can't create LibresourceEvent", e);
082: }
083: }
084:
085: public LibresourceEvent(URI fromResource, String service,
086: String resource, URI user, String eventType, String args)
087: throws LibresourceException {
088: try {
089: this .serviceName = service;
090: this .resourceType = resource;
091: this .throwedBy = user;
092: this .date = new Date();
093: this .eventType = eventType;
094: this .fromResource = fromResource;
095: this .args = args;
096: } catch (Exception e) {
097: throw new LibresourceException(
098: "Can't create LibresourceEvent", e);
099: }
100: }
101:
102: public LibresourceEvent(URI fromResource,
103: LibresourceResourceIdentifier identifier, URI user,
104: String eventType) throws LibresourceException {
105: this (fromResource, identifier, user, eventType, "");
106: }
107:
108: public LibresourceEvent(URI fromResource, String service,
109: String resource, URI user, String eventType)
110: throws LibresourceException {
111: this (fromResource, service, resource, user, eventType, "");
112: }
113:
114: public void toMessage(Message message) throws JMSException {
115: message.setStringProperty("fromResource",
116: normalizeURIForEvent(fromResource));
117: message.setStringProperty("eventType", eventType);
118: message.setStringProperty("serviceName", serviceName);
119: message.setStringProperty("resourceType", resourceType);
120: message.setStringProperty("throwedBy",
121: normalizeURIForEvent(throwedBy));
122: message.setStringProperty("date", Libresource.formatDate(date));
123: message.setStringProperty("args", args);
124: }
125:
126: public static LibresourceEvent toLibresourceEvent(Message message)
127: throws Exception {
128: LibresourceEvent event = new LibresourceEvent();
129: event.fromResource = new URI(message
130: .getStringProperty("fromResource"));
131: event.eventType = message.getStringProperty("eventType");
132: event.serviceName = message.getStringProperty("serviceName");
133: event.resourceType = message.getStringProperty("resourceType");
134: event.throwedBy = new URI(message
135: .getStringProperty("throwedBy"));
136: event.date = Libresource.parseDate(message
137: .getStringProperty("date"));
138: event.args = message.getStringProperty("args");
139:
140: return event;
141: }
142:
143: private static String normalizeURIForEvent(URI uri) {
144: String path = uri.normalize().getPath();
145:
146: if (!path.startsWith("/")) {
147: path = "/" + path;
148: }
149:
150: if (path.endsWith("/")) {
151: path = path.substring(0, path.length() - 1);
152: }
153:
154: return path;
155: }
156:
157: public Date getDate() {
158: return date;
159: }
160:
161: public String getEventType() {
162: return eventType;
163: }
164:
165: public URI getFromResource() {
166: return fromResource;
167: }
168:
169: public String getArgs() {
170: return args;
171: }
172:
173: public String getResourceType() {
174: return resourceType;
175: }
176:
177: public String getServiceName() {
178: return serviceName;
179: }
180:
181: public URI getThrowedBy() {
182: return throwedBy;
183: }
184: }
|