expanddeploygenerator.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,151 行 · 第 1/2 页
JAVA
1,151 行
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */package com.caucho.server.deploy;import com.caucho.config.ConfigException;import com.caucho.config.types.FileSetType;import com.caucho.config.types.Period;import com.caucho.git.GitRepository;import com.caucho.loader.Environment;import com.caucho.log.Log;import com.caucho.util.Alarm;import com.caucho.util.AlarmListener;import com.caucho.util.Crc64;import com.caucho.util.L10N;import com.caucho.util.WeakAlarm;import com.caucho.server.util.CauchoSystem;import com.caucho.vfs.Path;import java.io.IOException;import java.util.ArrayList;import java.util.Set;import java.util.TreeSet;import java.util.TreeMap;import java.util.logging.Level;import java.util.logging.Logger;/** * The generator for the deploy */abstract public class ExpandDeployGenerator<E extends ExpandDeployController> extends DeployGenerator<E> implements AlarmListener{ private static final Logger log = Logger.getLogger(ExpandDeployGenerator.class.getName()); private static final L10N L = new L10N(ExpandDeployGenerator.class); private static final long MIN_CRON_INTERVAL = 5000L; private Path _path; // default path private Path _containerRootDirectory; private Path _archiveDirectory; private Path _expandDirectory; private GitRepository _git; private String _gitPath; private String _extension = ".jar"; private String _expandPrefix = ""; private String _expandSuffix = ""; private boolean _isVersioning; private ArrayList<String> _requireFiles = new ArrayList<String>(); private TreeSet<String> _controllerNames = new TreeSet<String>(); private TreeMap<String,ArrayList<String>> _versionMap = new TreeMap<String,ArrayList<String>>(); private FileSetType _expandCleanupFileSet; private Alarm _alarm; private long _cronInterval; private volatile long _lastCheckTime; private volatile boolean _isChecking; private long _checkInterval = 1000L; private long _digest; private volatile boolean _isModified; private volatile boolean _isDeploying; /** * Creates the deploy. */ public ExpandDeployGenerator(DeployContainer<E> container, Path containerRootDirectory) { super(container); _containerRootDirectory = containerRootDirectory; _alarm = new WeakAlarm(this); _cronInterval = Environment.getDependencyCheckInterval(); if (_cronInterval < MIN_CRON_INTERVAL) _cronInterval = MIN_CRON_INTERVAL; } Path getContainerRootDirectory() { return _containerRootDirectory; } /** * Sets the war expand dir to check for new archive files. */ public void setArchiveDirectory(Path path) { _archiveDirectory = path; } /** * Gets the war expand directory. */ public Path getArchiveDirectory() { if (_archiveDirectory != null) return _archiveDirectory; else return _path; } /** * Returns the location for deploying an archive with the specified name. * * @param name a name, without an extension */ public Path getArchivePath(String name) { return getArchiveDirectory().lookup(name + getExtension()); } /** * Sets the war expand dir to check for new applications. */ public void setExpandPath(Path path) { log.config("Use <expand-directory> instead of <expand-path>. <expand-path> is deprecated."); setExpandDirectory(path); } /** * Sets the war expand dir to check for new applications. */ public void setExpandDirectory(Path path) { _expandDirectory = path; } /** * Gets the war expand directory. */ public Path getExpandDirectory() { if (_expandDirectory != null) return _expandDirectory; else return _path; } /** * Returns the location of an expanded archive, or null if no archive with * the passed name is deployed. * * @param name a name, without an extension */ public Path getExpandPath(String name) { if (!isDeployedKey(nameToEntryName(name))) return null; return getExpandDirectory().lookup(getExpandName(name)); /* if (expandDir.isDirectory()) return expandDir; Path extPath = getExpandDirectory().lookup(name + _extension); if (extPath.isDirectory()) return extPath; else return expandDir; */ } /** * Returns the combination of prefix, name, and suffix used for expanded * archives. * * @return */ protected String getExpandName(String name) { return getExpandPrefix() + name + getExpandSuffix(); } /** * Sets the dependency check interval. */ public void setDependencyCheckInterval(Period period) { _cronInterval = period.getPeriod(); if (_cronInterval < 0) _cronInterval = Period.INFINITE; else if (_cronInterval < MIN_CRON_INTERVAL) _cronInterval = MIN_CRON_INTERVAL; } public long getDependencyCheckInterval() { return _cronInterval; } /** * Sets the expand remove file set. */ public void setExpandCleanupFileset(FileSetType fileSet) { _expandCleanupFileSet = fileSet; } /** * Sets the extension. */ public void setExtension(String extension) throws ConfigException { if (! extension.startsWith(".")) throw new ConfigException(L.l("deployment extension '{0}' must begin with '.'", extension)); _extension = extension; } /** * Returns the extension. */ public String getExtension() { return _extension; } /** * Sets the expand prefix to check for new applications. */ public void setExpandPrefix(String prefix) throws ConfigException { if (! prefix.equals("") && ! prefix.startsWith("_") && ! prefix.startsWith(".")) throw new ConfigException(L.l("expand-prefix '{0}' must start with '.' or '_'.", prefix)); _expandPrefix = prefix; } /** * Gets the expand prefix. */ public String getExpandPrefix() { return _expandPrefix; } /** * Sets the expand suffix to check for new applications. */ public void setExpandSuffix(String suffix) throws ConfigException { _expandSuffix = suffix; } /** * Gets the expand suffix. */ public String getExpandSuffix() { return _expandSuffix; } /** * The Git repository */ public void setGit(GitRepository git) { _git = git; } /** * The Git repository */ public GitRepository getGit() { return _git; } /** * The Git ref directory */ public void setGitPath(String gitPath) { _gitPath = gitPath; } /** * The Git ref directory */ public String getGitPath() { return _gitPath; } /** * Gets the default path. */ public Path getPath() { return _path; } /** * Sets the deploy directory. */ public void setPath(Path path) { _path = path; } /** * Adds a required file in the expansion. */ public void addRequireFile(String file) throws ConfigException { _requireFiles.add(file); } /** * Sets true to enable versioning */ public void setVersioning(boolean isVersioning) { _isVersioning = isVersioning; } /** * Sets true to enable versioning */ public boolean isVersioning() { return _isVersioning; } /** * Returns the log. */ protected Logger getLog() { return log; } /** * Returns true if the deployment has modified. */ @Override public boolean isModified() { synchronized (this) { long now = Alarm.getCurrentTime(); if (now < _lastCheckTime + _checkInterval || _isChecking) { return _isModified; } _isChecking = true; _lastCheckTime = Alarm.getCurrentTime(); } try { long digest = getDigest(); _isModified = _digest != digest; return _isModified; } catch (Exception e) { log.log(Level.FINE, e.toString(), e); return false; } finally { _isChecking = false; } } /** * Log the reason for modification */ @Override public boolean logModified(Logger log) { long digest = getDigest(); if (_digest != digest) { String reason = ""; String name = getClass().getName(); int p = name.lastIndexOf('.'); if (p > 0) name = name.substring(p + 1); Path archiveDirectory = getArchiveDirectory(); if (archiveDirectory != null) reason = name + "[" + archiveDirectory.getNativePath() + "] is modified"; Path expandDirectory = getExpandDirectory(); if (expandDirectory != null && ! expandDirectory.equals(archiveDirectory)) { if (! "".equals(reason)) reason = reason + " or "; reason = name + "[" + expandDirectory.getNativePath() + "] is modified"; } log.info(reason); return true; } return false; } /** * Configuration checks on init. */ @Override protected void initImpl() throws ConfigException { super.initImpl(); if (getExpandDirectory() == null) throw new ConfigException(L.l("<expand-directory> must be specified for deployment of archive expansion.")); if (getArchiveDirectory() == null) throw new ConfigException(L.l("<archive-directory> must be specified for deployment of archive expansion.")); } /** * Starts the deploy. */ @Override protected void startImpl() { super.startImpl(); handleAlarm(_alarm); } /** * Returns the deployed keys. */ protected void fillDeployedKeys(Set<String> keys) { if (isModified()) { try { deploy(); } catch (Exception e) { log.log(Level.WARNING, e.toString(), e); } } for (String name : _controllerNames) { keys.add(name); } } /** * Return true for a matching key. */ protected boolean isDeployedKey(String key) { return _controllerNames.contains(key); } /** * Forces an update. */ public void update() { // force modify check _lastCheckTime = 0; request(); } /** * Redeploys if modified. */ public void request() { if (isModified()) { try { deploy(); } catch (Throwable e) { log.log(Level.WARNING, e.toString(), e); } } } /** * Deploys the objects. */ private void deploy() throws Exception { boolean isDeploying = false; log.finer(this + " redeploy " + _isDeploying); try { ArrayList<String> updatedNames = null; synchronized (this) { if (_isDeploying) return; else { _isDeploying = true; isDeploying = true; } TreeSet<String> entryNames = findEntryNames(); _digest = getDigest(); if (! _controllerNames.equals(entryNames)) { updatedNames = new ArrayList<String>(); for (String name : _controllerNames) { if (! entryNames.contains(name)) updatedNames.add(name); } for (String name : entryNames) { if (! _controllerNames.contains(name)) updatedNames.add(name); } _controllerNames = entryNames; } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?