/* */ package com.caucho.license;
/* */
/* */ import com.caucho.config.ConfigException;
/* */ import com.caucho.server.resin.Resin;
/* */ import com.caucho.util.CharBuffer;
/* */ import com.caucho.util.L10N;
/* */ import com.caucho.vfs.Vfs;
/* */ import java.io.File;
/* */ import java.io.IOException;
/* */ import java.util.ArrayList;
/* */ import java.util.Collection;
/* */ import java.util.Collections;
/* */ import java.util.Comparator;
/* */ import java.util.logging.Level;
/* */ import java.util.logging.Logger;
/* */
/* */ public class ProLicenseStore
/* */ extends LicenseStore
/* */ {
/* 30 */ private static final Logger log = Logger.getLogger(ProLicenseStore.class.getName());
/* 32 */ private static final L10N L = new L10N(ProLicenseStore.class);
/* 34 */ private ArrayList<File> _licensePath = new ArrayList();
/* 36 */ private ArrayList<LicenseImpl> _licenses = new ArrayList();
/* */ private File _licenseDirectory;
/* 40 */ private Collection<LicenseWrapper> _wrappers = new ArrayList();
/* 43 */ private String _licenseErrors = "";
/* */ private int _professionalCount = 1000;
/* */ private int _personalCount = 1000;
/* */
/* */ public ProLicenseStore()
/* */ {
/* */ try
/* */ {
/* 52 */ init(new File[0]);
/* */ }
/* */ catch (Exception e)
/* */ {
/* 54 */ log.log(Level.FINEST, e.toString(), e);
/* */
/* 56 */ this._licenseErrors = e.getMessage();
/* */ }
/* */ }
/* */
/* */ public ProLicenseStore(File... licenseDirectories)
/* */ throws ConfigException, IOException
/* */ {
/* 63 */ init(licenseDirectories);
/* */ }
/* */
/* */ public void clearLicenses()
/* */ {
/* 78 */ this._licenses.clear();
/* */ }
/* */
/* */ public final void setLicenseDirectory(File licenseDirectory)
/* */ {
/* 83 */ this._licenseDirectory = licenseDirectory;
/* */ }
/* */
/* */ public final void addLicenseDirectory(File licenseDirectory)
/* */ throws ConfigException, IOException
/* */ {
/* 95 */ if ((licenseDirectory == null) || (!licenseDirectory.canRead()) || (!licenseDirectory.isDirectory())) {
/* 98 */ return;
/* */ }
/* 101 */ if (this._licensePath.contains(licenseDirectory)) {
/* 102 */ return;
/* */ }
/* 105 */ if ((this._licenseDirectory == null) && (this._licensePath.size() == 0)) {
/* 106 */ this._licenseDirectory = licenseDirectory;
/* */ }
/* 109 */ this._licensePath.add(licenseDirectory);
/* */
/* 111 */ String[] list = licenseDirectory.list();
/* 115 */ if (list == null) {
/* 116 */ return;
/* */ }
/* 118 */ for (int i = 0; i < list.length; i++)
/* */ {
/* 119 */ String file = list[i];
/* 121 */ if (file.endsWith(".license")) {
/* */ try
/* */ {
/* 127 */ File license = new File(licenseDirectory, file);
/* 129 */ if (!license.canRead()) {
/* 130 */ throw new ConfigException(L.l("License file '{0}' is not readable.", license.toString()));
/* */ }
/* 134 */ LicenseImpl l = new LicenseImpl(license);
/* 136 */ if ((addLicense(l)) && (l.isValid()))
/* */ {
/* 137 */ this._professionalCount += l.getProfessionalCount();
/* 138 */ this._personalCount += l.getPersonalCount();
/* */ }
/* */ }
/* */ catch (Exception e)
/* */ {
/* 142 */ this._licenseErrors += e.getMessage();
/* */
/* 144 */ log.log(Level.FINER, e.toString(), e);
/* */
/* 146 */ log.info(e.toString());
/* */ }
/* */ }
/* */ }
/* 150 */ this._licenses.trimToSize();
/* 153 */ if (this._professionalCount > 0) {
/* 154 */ Vfs.initJNI();
/* */ }
/* */ }
/* */
/* */ final Collection<LicenseWrapper> getLicenses()
/* */ {
/* 159 */ ArrayList<LicenseWrapper> licenses = new ArrayList(this._wrappers);
/* */
/* 161 */ Collections.sort(licenses, new LicenseWrapperComparator());
/* */
/* 163 */ return Collections.unmodifiableCollection(licenses);
/* */ }
/* */
/* */ final Collection<License> getLicenseList()
/* */ {
/* 168 */ ArrayList<License> licenseList = new ArrayList();
/* 170 */ for (License license : this._licenses) {
/* 171 */ licenseList.add(new LicenseData(license));
/* */ }
/* 174 */ return licenseList;
/* */ }
/* */
/* */ private boolean addLicense(LicenseImpl license)
/* */ {
/* 179 */ for (LicenseImpl testLicense : this._licenses) {
/* 180 */ if ((testLicense.getSignature().equals(license.getSignature())) && (testLicense.getSignature31().equals(license.getSignature31()))) {
/* 182 */ return false;
/* */ }
/* */ }
/* 186 */ this._licenses.add(license);
/* 187 */ this._wrappers.add(new LicenseWrapper(license));
/* */
/* 189 */ return true;
/* */ }
/* */
/* */ int getProfessionalCount()
/* */ {
/* 197 */ return this._professionalCount;
/* */ }
/* */
/* */ int getPersonalCount()
/* */ {
/* 205 */ return this._personalCount;
/* */ }
/* */
/* */ public File getLicenseDirectory()
/* */ {
/* 211 */ return this._licenseDirectory;
/* */ }
/* */
/* */ final void validate(int professionalCount, int personalCount)
/* */ throws ConfigException
/* */ {
/* 221 */ if ((professionalCount <= getProfessionalCount()) && ((personalCount <= getPersonalCount()) || (personalCount <= getProfessionalCount()))) {
/* 224 */ return;
/* */ }
/* 227 */ String licenseDirectory = null;
/* 229 */ if (this._licensePath.size() > 0)
/* */ {
/* 230 */ licenseDirectory = ((File)this._licensePath.get(0)).toString();
/* */ }
/* */ else
/* */ {
/* 233 */ Resin resin = Resin.getCurrent();
/* 235 */ if (resin != null) {
/* 236 */ licenseDirectory = Resin.getCurrent().getConfDirectory() + "/licenses";
/* */ }
/* */ }
/* 239 */ String msg = null;
/* 240 */ if (getProfessionalCount() + getPersonalCount() < 1) {
/* 241 */ msg = L.l(" Resin Professional has not found any valid licenses.\n Licenses belong in {0}.\n See http://www.caucho.com/resin/sales for licensing information.\n{1}", licenseDirectory, getFullDescription());
/* 247 */ } else if (getProfessionalCount() < professionalCount) {
/* 248 */ msg = L.l(" Resin Professional has found fewer licenses ({0}) than required ({1}).\n Licenses belong in {2}.\n See http://www.caucho.com/resin/sales for purchasing information.\n{3}", Integer.valueOf(getProfessionalCount()), Integer.valueOf(professionalCount), licenseDirectory, getFullDescription());
/* 254 */ } else if ((getPersonalCount() < personalCount) && (getProfessionalCount() < personalCount)) {
/* 256 */ msg = L.l(" Resin Personal has found fewer licenses ({0}) than required ({1}).\n Licenses belong in {2}.\n See http://www.caucho.com/resin/sales for purchasing information.\n{3}", Integer.valueOf(getPersonalCount()), Integer.valueOf(personalCount), licenseDirectory, getFullDescription());
/* */ }
/* 263 */ throw new ConfigException(msg);
/* */ }
/* */
/* */ String doLogging()
/* */ {
/* 268 */ StringBuilder sb = new StringBuilder();
/* */