KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > security > license > DaysLeftValidator


1 package org.jahia.security.license;
2
3 import org.jahia.settings.SettingsBean;
4 import org.jahia.bin.Jahia;
5 import java.util.Map JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.io.FileInputStream JavaDoc;
8 import java.io.File JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.ObjectInputStream JavaDoc;
11 import java.io.FileOutputStream JavaDoc;
12 import java.io.ObjectOutputStream JavaDoc;
13 import java.util.Date JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.io.EOFException JavaDoc;
16 import org.jahia.resourcebundle.ResourceMessage;
17
18 /**
19  * <p>Title: </p>
20  * <p>Description: </p>
21  * <p>Copyright: Copyright (c) 2002</p>
22  * <p>Company: Jahia Ltd</p>
23  * @author Serge Huber
24  * @version 1.0
25  */

26
27 public class DaysLeftValidator extends AbstractValidator {
28
29     private static org.apache.log4j.Logger logger =
30         org.apache.log4j.Logger.getLogger(DaysLeftValidator.class);
31
32     static private final String JavaDoc DAYSLEFT_FILE = "jahia.png";
33
34     private SettingsBean settingsBean;
35     private Map JavaDoc componentDaysLeft = new HashMap JavaDoc();
36     private Date JavaDoc componentInstallDate = null;
37     private boolean fileLoaded = false;
38
39     public DaysLeftValidator (String JavaDoc name, String JavaDoc value, License license) {
40         super(name, value, license);
41     }
42
43     public boolean assertEquals (String JavaDoc value) {
44         checkSettings();
45         int totalAllowedDays = Integer.parseInt(value);
46         Date JavaDoc nowDate = new Date JavaDoc();
47         long nowDateLong = nowDate.getTime();
48         long installDateLong = componentInstallDate.getTime();
49         long maxDateLong = installDateLong + 1000L*60L*60L*24L*totalAllowedDays;
50         if (nowDateLong > maxDateLong) {
51             errorMessage = new ResourceMessage("org.jahia.security.license.DaysLeftValidator.daysLeftExpired.label", new Integer JavaDoc(totalAllowedDays));
52             return false;
53         } else {
54             return true;
55         }
56     }
57
58     public boolean assertInRange (String JavaDoc fromValue, String JavaDoc toValue) {
59         checkSettings();
60         int minAllowedDays = Integer.parseInt(fromValue);
61         int maxAllowedDays = Integer.parseInt(toValue);
62         Date JavaDoc nowDate = new Date JavaDoc();
63         long nowDateLong = nowDate.getTime();
64         long installDateLong = componentInstallDate.getTime();
65         long minDateLong = installDateLong + 1000L*60L*60L*24L*minAllowedDays;
66         long maxDateLong = installDateLong + 1000L*60L*60L*24L*maxAllowedDays;
67         if ((nowDateLong > maxDateLong) || (nowDateLong < minDateLong)) {
68             errorMessage = new ResourceMessage("org.jahia.security.license.DaysLeftValidator.daysLeftNotInRange.label", new Integer JavaDoc(minAllowedDays), new Integer JavaDoc(maxAllowedDays));
69             return false;
70         } else {
71             return true;
72         }
73     }
74
75     private void checkSettings() {
76         if (settingsBean == null) {
77             settingsBean = Jahia.getSettings();
78         }
79         if (fileLoaded) {
80             return;
81         }
82         String JavaDoc fullFilePath = settingsBean.getJahiaEtcDiskPath() +
83             File.separator + "config" +
84             File.separator + DAYSLEFT_FILE;
85         File JavaDoc daysLeftFile = new File JavaDoc(fullFilePath);
86         if (daysLeftFile.exists()) {
87             try {
88                 FileInputStream JavaDoc fileIn = new FileInputStream JavaDoc(daysLeftFile);
89                 ObjectInputStream JavaDoc objectIn = new ObjectInputStream JavaDoc(fileIn);
90                 try {
91                     while (true) {
92                         String JavaDoc componentName = (String JavaDoc) objectIn.readObject();
93                         long installDateLong = objectIn.readLong();
94                         componentDaysLeft.put(componentName, new Date JavaDoc(installDateLong));
95                     }
96                 } catch (EOFException JavaDoc eofe) {
97                     // we are going to assume this error is due to reaching the end of
98
// the file.
99
}
100             } catch (IOException JavaDoc ioe) {
101                 logger.error("Error while loading days left file [" +
102                              fullFilePath + "]", ioe);
103             } catch (ClassNotFoundException JavaDoc cnfe) {
104                 logger.error("Error while loading days left file [" +
105                              fullFilePath + "]", cnfe);
106             }
107         }
108         // do we have an entry for our component name ? If not let's add one
109
// with the current date.
110
if (!componentDaysLeft.containsKey(license.getComponentName())) {
111             componentDaysLeft.put(license.getComponentName(), new Date JavaDoc());
112             try {
113                 // now let's update the data in the file if needed
114
FileOutputStream JavaDoc fileOut = new FileOutputStream JavaDoc(daysLeftFile);
115                 ObjectOutputStream JavaDoc objectOut = new ObjectOutputStream JavaDoc(fileOut);
116                 Iterator JavaDoc componentNameIter = componentDaysLeft.keySet().iterator();
117                 while (componentNameIter.hasNext()) {
118                     String JavaDoc curComponentName = (String JavaDoc) componentNameIter.next();
119                     Date JavaDoc daysLeftDate = (Date JavaDoc) componentDaysLeft.get(curComponentName);
120                     objectOut.writeObject(curComponentName);
121                     objectOut.writeLong(daysLeftDate.getTime());
122                 }
123                 objectOut.flush();
124                 objectOut.close();
125             } catch (IOException JavaDoc ioe) {
126                 logger.error("Error while updating days left file [" +
127                              fullFilePath + "]", ioe);
128             }
129         }
130
131         componentInstallDate = (Date JavaDoc) componentDaysLeft.get(license.getComponentName());
132
133         fileLoaded=true;
134     }
135
136     public Date JavaDoc getComponentInstallDate() {
137         return componentInstallDate;
138     }
139
140 }
Popular Tags