KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.util.license;
2
3 import java.io.IOException JavaDoc;
4 import java.io.Serializable JavaDoc;
5 import java.security.KeyPair JavaDoc;
6 import java.security.PrivateKey JavaDoc;
7 import java.security.PublicKey JavaDoc;
8 import java.util.Date JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.Map JavaDoc;
11
12 import org.sapia.util.CompositeRuntimeException;
13
14 /**
15  * A license registration holds all data pertaining to a given license. An instance of this class internally keeps a <code>LicenseRecord</code>
16  * that should normally be given to a specific licensee. It also holds the <code>KeyPair</code> (private and public keys) that belongs to the
17  * licensee. It is normally kept at the licensing party, and associated to some licensee account (typically, an instance of this class could be
18  * kept in a database, as a blob).
19  * <p>
20  * An instance of this class can be used to acquire the license corresponding to a licensee, or to create a new license for that licensee.
21  * Typically, applications will create a new license for a licensee programmatically, and send that license electronically (normally by email)
22  * to that licensee.
23  *
24  * @author Yanick Duchesne
25  *
26  * @see org.sapia.util.license.LicenseRegistrationFactory
27  *
28  * <dl>
29  * <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>
30  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
31  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
32  * </dl>
33  */

34 public class LicenseRegistration implements Serializable JavaDoc{
35   
36   static final long serialVersionUID = 1L;
37   
38   private LicenseRecord _license;
39   private transient PublicKey JavaDoc _pub;
40   private transient PrivateKey JavaDoc _priv;
41   private byte[] _pubBytes, _privBytes;
42   private Map JavaDoc _data;
43   private Date JavaDoc _lastUpdate;
44   private Object JavaDoc _vendorId;
45   
46   LicenseRegistration(Object JavaDoc vendorId, LicenseRecord license, KeyPair JavaDoc keys, Map JavaDoc data, Date JavaDoc createDate){
47     _vendorId = vendorId;
48     _license = license;
49     _pubBytes = keys.getPublic().getEncoded();
50     _privBytes = keys.getPrivate().getEncoded();
51     _data = data;
52     _lastUpdate = createDate;
53   }
54   
55   LicenseRegistration(Object JavaDoc vendorId, LicenseRecord license, KeyPair JavaDoc keys, Date JavaDoc createDate){
56     this(vendorId, license, keys, new HashMap JavaDoc(), createDate);
57   }
58   
59   /**
60    * @return the <code>PublicKey</code> that this instance holds.
61    */

62   public PublicKey JavaDoc getPublic(SecurityFactory fac){
63     initKeys(fac);
64     return _pub;
65   }
66   
67   /**
68    * @return the <code>PrivateKey</code> that this instance holds.
69    */

70   public PrivateKey JavaDoc getPrivateKey(SecurityFactory fac){
71     initKeys(fac);
72     return _priv;
73   }
74   
75   /**
76    * @return the <code>Date</code> at which this instance was created, or
77    * at which its encpasulated license was modified.
78    *
79    * @see #registerNewLicense(License)
80    */

81   public Date JavaDoc getLastUpdate(){
82     return _lastUpdate;
83   }
84   
85   /**
86    * @return the <code>LicenseRecord</code> that this instance holds.
87    */

88   public LicenseRecord getLicenseRecord(){
89     return _license;
90   }
91   
92   /**
93    * @param name a binding name.
94    * @return the <code>Object</code> for the given name.
95    */

96   public Object JavaDoc getBinding(String JavaDoc name){
97     if(_data == null)
98       return null;
99     return _data.get(name);
100   }
101   
102   /**
103    * @param license a <code>License</code> object.
104    * @param currentDate a <code>Date</code> object representing the current date.
105    * @param factory the <code>SignatureFactory</code> internally used to sign the license's bytes.
106    * @param idFactory the <code>LicenseIdFactory</code> internally used to create the license identifier.
107    * @throws IOException if an IO problem occurs while registering the license.
108    * @throws Exception if a low level problem occurs.
109    */

110   public void registerNewLicense(License license, Date JavaDoc currentDate, SecurityFactory factory, LicenseIdFactory idFactory) throws IOException JavaDoc, Exception JavaDoc{
111     _lastUpdate = currentDate;
112     initKeys(factory);
113     _license.registerNewLicense(_vendorId, license, _pub, _priv, factory, idFactory);
114   }
115   
116   private void initKeys(SecurityFactory fac){
117     if(_pub == null || _priv == null){
118       try{
119         _pub = (PublicKey JavaDoc)fac.generatePublic(_pubBytes);
120         _priv = (PrivateKey JavaDoc)fac.generatePrivate(_privBytes);
121       }catch(Exception JavaDoc e){
122         throw new CompositeRuntimeException("Could not generate key from bytes", e);
123       }
124     }
125   }
126
127 }
128
Popular Tags