1 package org.jahia.security.license; 2 3 import org.jahia.settings.SettingsBean; 4 import org.jahia.bin.Jahia; 5 import java.util.Map ; 6 import java.util.HashMap ; 7 import java.io.FileInputStream ; 8 import java.io.File ; 9 import java.io.IOException ; 10 import java.io.ObjectInputStream ; 11 import java.io.FileOutputStream ; 12 import java.io.ObjectOutputStream ; 13 import java.util.Date ; 14 import java.util.Iterator ; 15 import java.io.EOFException ; 16 import org.jahia.resourcebundle.ResourceMessage; 17 18 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 DAYSLEFT_FILE = "jahia.png"; 33 34 private SettingsBean settingsBean; 35 private Map componentDaysLeft = new HashMap (); 36 private Date componentInstallDate = null; 37 private boolean fileLoaded = false; 38 39 public DaysLeftValidator (String name, String value, License license) { 40 super(name, value, license); 41 } 42 43 public boolean assertEquals (String value) { 44 checkSettings(); 45 int totalAllowedDays = Integer.parseInt(value); 46 Date nowDate = new Date (); 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 (totalAllowedDays)); 52 return false; 53 } else { 54 return true; 55 } 56 } 57 58 public boolean assertInRange (String fromValue, String toValue) { 59 checkSettings(); 60 int minAllowedDays = Integer.parseInt(fromValue); 61 int maxAllowedDays = Integer.parseInt(toValue); 62 Date nowDate = new Date (); 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 (minAllowedDays), new Integer (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 fullFilePath = settingsBean.getJahiaEtcDiskPath() + 83 File.separator + "config" + 84 File.separator + DAYSLEFT_FILE; 85 File daysLeftFile = new File (fullFilePath); 86 if (daysLeftFile.exists()) { 87 try { 88 FileInputStream fileIn = new FileInputStream (daysLeftFile); 89 ObjectInputStream objectIn = new ObjectInputStream (fileIn); 90 try { 91 while (true) { 92 String componentName = (String ) objectIn.readObject(); 93 long installDateLong = objectIn.readLong(); 94 componentDaysLeft.put(componentName, new Date (installDateLong)); 95 } 96 } catch (EOFException eofe) { 97 } 100 } catch (IOException ioe) { 101 logger.error("Error while loading days left file [" + 102 fullFilePath + "]", ioe); 103 } catch (ClassNotFoundException cnfe) { 104 logger.error("Error while loading days left file [" + 105 fullFilePath + "]", cnfe); 106 } 107 } 108 if (!componentDaysLeft.containsKey(license.getComponentName())) { 111 componentDaysLeft.put(license.getComponentName(), new Date ()); 112 try { 113 FileOutputStream fileOut = new FileOutputStream (daysLeftFile); 115 ObjectOutputStream objectOut = new ObjectOutputStream (fileOut); 116 Iterator componentNameIter = componentDaysLeft.keySet().iterator(); 117 while (componentNameIter.hasNext()) { 118 String curComponentName = (String ) componentNameIter.next(); 119 Date daysLeftDate = (Date ) componentDaysLeft.get(curComponentName); 120 objectOut.writeObject(curComponentName); 121 objectOut.writeLong(daysLeftDate.getTime()); 122 } 123 objectOut.flush(); 124 objectOut.close(); 125 } catch (IOException ioe) { 126 logger.error("Error while updating days left file [" + 127 fullFilePath + "]", ioe); 128 } 129 } 130 131 componentInstallDate = (Date ) componentDaysLeft.get(license.getComponentName()); 132 133 fileLoaded=true; 134 } 135 136 public Date getComponentInstallDate() { 137 return componentInstallDate; 138 } 139 140 } | Popular Tags |