Source code: com/sourcetap/license/License.java
1 /*
2 * $Id$
3 *
4 * Copyright (c) 2003 SourceTap - www.sourcetap.com
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21 * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24 package com.sourcetap.license;
25
26 import java.util.Date;
27 import java.text.DateFormat;
28 import java.util.Locale;
29
30
31 /**
32 * License Information. Basic information (company Name, email Address, and expiration date) and a license key
33 * is encapsulated within.
34 *
35 * @author Steve Fowler
36 * @version $Revision$
37 */
38 public class License
39 {
40
41 /**
42 * constructor to create a license from the specified license key
43 *
44 * @param licenseKey the unencrypted version of the license key
45 * @throws LicenseException
46 */
47 public License(String licenseKey)
48 throws LicenseException
49 {
50 setLicenseKey(licenseKey);
51 }
52
53 /**
54 * constructor to create a license from the specified license data.
55 *
56 * @param companyName
57 * @param email
58 * @param expirationDate date should be passed in in SHORT format for US locale
59 * @throws LicenseException
60 */
61 public License(String companyName,
62 String email, String expirationDate )
63 throws LicenseException
64 {
65 setCompanyName(companyName);
66 setEmailAddress(email);
67 setExpirationDate(expirationDate);
68 }
69
70 /**
71 * constructor to create a license from the specified license data.
72 *
73 * @param companyName
74 * @param email
75 * @param expirationDate
76 * @throws LicenseException
77 */
78 public License(String companyName,
79 String email, Date expirationDate )
80 {
81 setCompanyName(companyName);
82 setEmailAddress(email);
83 setExpirationDate(expirationDate);
84 }
85
86 public String getCompanyName()
87 {
88 return companyName;
89 }
90
91 public void setCompanyName( String companyName )
92 {
93 this.companyName = companyName;
94 }
95
96 public void setLicenseKey(String licenseKey)
97 throws LicenseException
98 {
99 String tokens[] = licenseKey.split("#");
100 if(tokens.length != 3)
101 throw new LicenseException("LicenseKey is invalid");
102 setCompanyName(tokens[0]);
103 setEmailAddress(tokens[1]);
104 setExpirationDate(tokens[2]);
105 }
106 public String getLicenseKey()
107 {
108 return companyName + "#" + email + "#" + getExpirationDateString();
109 }
110
111 public String getEmailAddress() {
112 return this.email;
113 }
114 public void setEmailAddress(String email) {
115 this.email=email;
116 }
117 public Date getExpirationDate() {
118 return this.expirationDate;
119 }
120
121 public String getExpirationDateString()
122 {
123 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
124 return df.format(expirationDate);
125 }
126 /**
127 * parses the string and converts it to a date
128 */
129 public void setExpirationDate(String expirationDateStr) throws LicenseException{
130 try
131 {
132 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
133 Date date = df.parse(expirationDateStr);
134 this.expirationDate=date;
135 }
136 catch ( Exception e)
137 {
138 throw new LicenseException( e.getMessage() );
139 }
140 }
141 public void setExpirationDate(Date expirationDate) {
142 this.expirationDate=expirationDate;
143 }
144
145
146 /**
147 *
148 * determine if the license is expired.
149 * @return true if license is expired
150 */
151 public boolean isExpired()
152 {
153 return System.currentTimeMillis() > expirationDate.getTime();
154 }
155
156 private String companyName;
157 private String email;
158 private Date expirationDate;
159
160 }