KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > util > license > LicenseRegistrationFactory


1 package org.sapia.util.license;
2
3 import java.io.IOException JavaDoc;
4 import java.security.KeyPair JavaDoc;
5 import java.security.KeyPairGenerator JavaDoc;
6 import java.security.SecureRandom JavaDoc;
7 import java.util.Date JavaDoc;
8 import java.util.Map JavaDoc;
9
10 /**
11  * This factory is used to create <code>LicenseRegistration</code>s. The created
12  * registrations are handled by client applications. Registrations should normally be kept in a
13  * database, on a per-client basis, on the vendor's side.
14  * <p>
15  * The code snippet below illlustrates how an instance of this class can be used:
16  * <pre>
17  * DurationLicense license = new DurationLicense(new Date(), 30);
18  * LicenseRegistration registration = LicenseRegistrationFactory.createRegistration(someVendorIdentifier, license);
19  *
20  * // ... here save registration as blob in database ...
21  *
22  * // the license record below is normally sent to the licensee (for example, it can be sent
23  * // by mail, as a serialized object (in an attached file), or in some form of proprietary
24  * // marshalling format. A LicenseRecord is normally processed within application code at the client,
25  * // to enforce usage rights.
26  * LicenseRecord toSend = registration.getLicenseRecord();
27  *
28  * </pre>
29  * <p>
30  * Note that an instance of this class keeps an application-specific vendor identifier. That identifier is also encapsulated
31  * within license records sent to clients. The vendor identifier is kept on the client side to make sure that eventual licenses
32  * on the client side are eventually updated with licenses coming from the original vendor.
33  *
34  * @author Yanick Duchesne
35  *
36  * <dl>
37  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
38  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
39  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
40  * </dl>
41  */

42 public class LicenseRegistrationFactory {
43   
44   private SecurityFactory _fac;
45   
46   public LicenseRegistrationFactory(SecurityFactory fac){
47     _fac = fac;
48   }
49   
50   public LicenseRegistrationFactory(){
51     this(new SecurityFactory());
52   }
53   
54   /**
55    * @return the <code>SecurityFactory</code> that this instance uses.
56    */

57   public SecurityFactory getSecurityFactory(){
58     return _fac;
59   }
60   
61   /**
62    * Creates a <code>LicenseRegistration</code> and returns it.
63    * @param serializableVendorId the identifier of the vendor that emits the <code>License</code>.
64    * @param license a <code>License</code> object.
65    * @param data an <code>Map</code> of arbitrary serializable bindings kept within the returned registration.
66    * @param currentDate a <code>Date</code> object representing the current date.
67    * @param idFactory the <code>LicenseIdFactory</code> internally used to create the license identifier.
68    * @return a <code>LicenseRegistration</code>.
69    * @throws IOException if an IO problem occurs while creating the registration.
70    * @throws Exception if some low-level problem occurs.
71    */

72   public LicenseRegistration createRegistration(Object JavaDoc serializableVendorId, License license, Date JavaDoc currentDate, LicenseIdFactory idFactory) throws IOException JavaDoc, Exception JavaDoc{
73     return createRegistration(serializableVendorId, 1024, license, currentDate, idFactory);
74   }
75   
76   /**
77    * Creates a <code>LicenseRegistration</code> and returns it.
78    * @param serializableVendorId the identifier of the vendor that emits the <code>License</code>.
79    * @param license a <code>License</code> object.
80    * @param serializableData an <code>Map</code> of arbitrary serializable bindings kept within the returned registration.
81    * @param currentDate a <code>Date</code> object representing the current date.
82    * @param idFactory the <code>LicenseIdFactory</code> internally used to create the license identifier.
83    * @return a <code>LicenseRegistration</code>.
84    * @throws IOException if an IO problem occurs while creating the registration.
85    * @throws Exception if some low-level problem occurs.
86    */

87   public LicenseRegistration createRegistration(Object JavaDoc serializableVendorId,
88                                                 License license,
89                                                 Map JavaDoc serializableData,
90                                                 Date JavaDoc currentDate,
91                                                 LicenseIdFactory idFactory) throws IOException JavaDoc, Exception JavaDoc{
92     return createRegistration(serializableVendorId, 1024, license, serializableData, currentDate, idFactory);
93   }
94   
95   /**
96    * Creates a <code>LicenseRegistration</code> and returns it.
97    *
98    * @param serializableVendorId the identifier of the vendor that emits the <code>License</code>.
99    * @param license a <code>License</code> object.
100    * @param currentDate a <code>Date</code> object representing the current date.
101    * @param idFactory the <code>LicenseIdFactory</code> internally used to create the license identifier.
102    * @return a <code>LicenseRegistration</code>.
103    * @throws IOException if an IO problem occurs while creating the registration.
104    * @throws Exception if some low-level problem occurs.
105    */

106   public LicenseRegistration createRegistration(Object JavaDoc serializableVendorId,
107                                                 int strength,
108                                                 License license,
109                                                 Date JavaDoc currentDate,
110                                                 LicenseIdFactory idFactory) throws IOException JavaDoc, Exception JavaDoc{
111     return createRegistration(serializableVendorId, strength, license, null, currentDate, idFactory);
112   }
113   
114   /**
115    * Creates a <code>LicenseRegistration</code> and returns it.
116    * @param strength the "strength" (number of bytes) of the internal encryption keys.
117    * @param serializableVendorId the identifier of the vendor that emits the <code>License</code>.
118    * @param license a <code>License</code>
119    * @param serializableData an <code>Map</code> of arbitrary serializable bindings kept within the returned registration.
120    * @param currentDate a <code>Date</code> object representing the current date.
121    * @param idFactory the <code>LicenseIdFactory</code> internally used to create the license identifier.
122    * @return a <code>LicenseRegistration</code>
123    * @throws IOException if an IO problem occurs while creating the registration.
124    * @throws Exception if some low-level problem occurs.
125    */

126   public LicenseRegistration createRegistration(Object JavaDoc serializableVendorId,
127                                                 int strength,
128                                                 License license,
129                                                 Map JavaDoc serializableData,
130                                                 Date JavaDoc currentDate,
131                                                 LicenseIdFactory idFactory) throws IOException JavaDoc, Exception JavaDoc{
132     KeyPairGenerator JavaDoc kpGen = _fac.newKeyPairGenerator("DSA");
133     kpGen.initialize(strength, new SecureRandom JavaDoc());
134     KeyPair JavaDoc kp = kpGen.generateKeyPair();
135     LicenseRecord rec = LicenseRecord.newInstance(serializableVendorId, license, kp.getPublic(), kp.getPrivate(), _fac, idFactory);
136     return new LicenseRegistration(serializableVendorId, rec, kp, serializableData, currentDate);
137   }
138   
139 }
140
Popular Tags