Source code: com/sourcetap/license/LicenseManagerImpl.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.*;
27 import java.io.*;
28
29 /**
30 * LicenseManager implementation.
31 *
32 * @author Steve Fowler
33 * @version $Revision$
34 */
35 public class LicenseManagerImpl
36 implements LicenseManager
37 {
38 private static LicenseManager licenseManager;
39 Properties props;
40 License license = null;
41
42 public static LicenseManager getInstance()
43 {
44 if(licenseManager == null)
45 synchronized(com.sourcetap.license.LicenseManagerImpl.class)
46 {
47 if(licenseManager == null)
48 licenseManager = new LicenseManagerImpl();
49 }
50 return licenseManager;
51 }
52
53
54 public LicenseManagerImpl()
55 {
56 license = null;
57 PropertyUtil pu = new PropertyUtil();
58 props = pu.getProperties();
59 }
60
61 public boolean hasValidLicense() throws LicenseException
62 {
63 License license = getLicense();
64
65 return !license.isExpired();
66 }
67
68 public License getLicense() throws LicenseException
69 {
70 if(license != null)
71 return license;
72 try
73 {
74
75 FileInputStream licfis = new FileInputStream(props.getProperty("licenseFile", "license.dat"));
76 byte[] licBytes = new byte[licfis.available()];
77 licfis.read(licBytes);
78 licfis.close();
79
80 String licenseString = new String(licBytes);
81
82 // verify license
83 License license = LicenseVerifier.validateLicense( licenseString, props.getProperty("publicKeyFile", "public.key"));
84 return license;
85 }
86 catch (Exception e)
87 {
88 throw new LicenseException("get License Error: " + e.getMessage());
89 }
90 }
91
92 /**
93 * The licenseKey should be passed in as generated by the Registration Server
94 * @see com.sourcetap.license.LicenseManager#setLicense(java.lang.String)
95 */
96 public void setLicense( String licenseKey ) throws LicenseException
97 {
98 try {
99 byte[] enckey = licenseKey.getBytes();
100 FileOutputStream licfos = new FileOutputStream(props.getProperty("licenseFile", "license.dat"));
101 licfos.write(enckey);
102 licfos.close();
103 }
104 catch (Exception e)
105 {
106 throw new LicenseException("Unable to save license file");
107 }
108 }
109
110 }