001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.deployment.repository.util;
021:
022: import org.apache.axis2.deployment.DeploymentConstants;
023: import org.apache.axis2.deployment.DeploymentEngine;
024: import org.apache.axis2.deployment.Deployer;
025:
026: import java.io.File;
027: import java.util.*;
028:
029: public class WSInfoList implements DeploymentConstants {
030:
031: /**
032: * This is to store all the jar files in a specified folder (WEB_INF)
033: */
034: private List jarList = new ArrayList();
035:
036: /**
037: * All the currently updated jars
038: */
039: public Map currentJars = new HashMap();
040:
041: /**
042: * Reference to DeploymentEngine to make update
043: */
044:
045: private boolean locked = false;
046: private final DeploymentEngine deploymentEngine;
047:
048: public WSInfoList(DeploymentEngine deploy_engine) {
049: deploymentEngine = deploy_engine;
050: }
051:
052: /**
053: * First checks whether the file is already available by the
054: * system call fileExists. If it is not deployed yet then adds to the jarList
055: * and to the deployment engine as a new service or module.
056: * While adding new item to jarList, first creates the WSInfo object and
057: * then adds to the jarlist and actual jar file is added to DeploymentEngine.
058: * <p/>
059: * If the files already exists, then checks whether it has been updated
060: * then changes the last update date of the wsInfo and adds two entries to
061: * DeploymentEngine - one for new deployment and other for undeployment.
062: *
063: * @param file actual jar files for either Module or service
064: */
065: public synchronized void addWSInfoItem(File file,
066: Deployer deployer, int type) {
067: WSInfo info = (WSInfo) currentJars.get(file.getName());
068: if (info != null) {
069: if (deploymentEngine.isHotUpdate()
070: && isModified(file, info)) {
071: // info.setLastModifiedDate(file.lastModified());
072: WSInfo wsInfo = new WSInfo(info.getFileName(), info
073: .getLastModifiedDate(), deployer, type);
074: deploymentEngine.addWSToUndeploy(wsInfo); // add entry to undeploy list
075: DeploymentFileData deploymentFileData = new DeploymentFileData(
076: file, deployer);
077: deploymentEngine.addWSToDeploy(deploymentFileData); // add entry to deploylist
078: }
079: } else {
080: info = getFileItem(file, deployer, type);
081: setLastModifiedDate(file, info);
082: }
083:
084: jarList.add(info.getFileName());
085: }
086:
087: /**
088: * Checks undeployed Services. Checks old jars files and current jars.
089: * If name of the old jar file does not exist in the current jar
090: * list then it is assumed that the jar file has been removed
091: * and that is hot undeployment.
092: */
093: private synchronized void checkForUndeployedServices() {
094: if (!locked) {
095: locked = true;
096: } else {
097: return;
098: }
099: Iterator infoItems = currentJars.keySet().iterator();
100: List tobeRemoved = new ArrayList();
101: while (infoItems.hasNext()) {
102: String fileName = (String) infoItems.next();
103: WSInfo infoItem = (WSInfo) currentJars.get(fileName);
104: if (infoItem.getType() == WSInfo.TYPE_MODULE) {
105: continue;
106: }
107: //seems like someone has deleted the file , so need to undeploy
108: boolean found = false;
109: for (int i = 0; i < jarList.size(); i++) {
110: String s = (String) jarList.get(i);
111: if (fileName.equals(s)) {
112: found = true;
113: }
114: }
115: if (!found) {
116: tobeRemoved.add(fileName);
117: deploymentEngine.addWSToUndeploy(infoItem);
118: }
119: }
120:
121: for (int i = 0; i < tobeRemoved.size(); i++) {
122: String fileName = (String) tobeRemoved.get(i);
123: currentJars.remove(fileName);
124: }
125: tobeRemoved.clear();
126: jarList.clear();
127: locked = false;
128: }
129:
130: /**
131: * Clears the jarlist.
132: */
133: public void init() {
134: jarList.clear();
135: }
136:
137: /**
138: *
139: */
140: public void update() {
141: synchronized (deploymentEngine) {
142: checkForUndeployedServices();
143: deploymentEngine.unDeploy();
144: deploymentEngine.doDeploy();
145: }
146: }
147:
148: /**
149: * Gets the WSInfo object related to a file if it exists, null otherwise.
150: *
151: */
152: private WSInfo getFileItem(File file, Deployer deployer, int type) {
153: String fileName = file.getName();
154: WSInfo info = (WSInfo) currentJars.get(fileName);
155: if (info == null) {
156: info = new WSInfo(file.getName(), file.lastModified(),
157: deployer, type);
158: currentJars.put(file.getName(), info);
159: DeploymentFileData fileData = new DeploymentFileData(file,
160: deployer);
161: deploymentEngine.addWSToDeploy(fileData);
162: }
163: return info;
164: }
165:
166: /**
167: * Checks if a file has been modified by comparing the last update date of
168: * both files and WSInfo. If they are different, the file is assumed to have
169: * been modified.
170: *
171: * @param file
172: * @param wsInfo
173: */
174: private boolean isModified(File file, WSInfo wsInfo) {
175: if (file.isDirectory()) {
176: if (isChanged(file, wsInfo.getLastModifiedDate(), wsInfo)) {
177: setLastModifiedDate(file, wsInfo);
178: return true;
179: } else {
180: return false;
181: }
182: } else {
183: if (wsInfo.getLastModifiedDate() != file.lastModified()) {
184: wsInfo.setLastModifiedDate(file.lastModified());
185: return true;
186: } else {
187: return false;
188: }
189: }
190: }
191:
192: private void setLastModifiedDate(File file, WSInfo wsInfo) {
193: if (file.isDirectory()) {
194: File files[] = file.listFiles();
195: for (int i = 0; i < files.length; i++) {
196: File fileItem = files[i];
197: if (fileItem.isDirectory()) {
198: setLastModifiedDate(fileItem, wsInfo);
199: } else {
200: fileItem.setLastModified(wsInfo
201: .getLastModifiedDate());
202: }
203: }
204: } else {
205: file.setLastModified(wsInfo.getLastModifiedDate());
206: }
207: }
208:
209: private boolean isChanged(File file, long lastModifedData,
210: WSInfo wsInfo) {
211: File files[] = file.listFiles();
212: for (int i = 0; i < files.length; i++) {
213: File fileItem = files[i];
214: if (fileItem.isDirectory()) {
215: if (isChanged(fileItem, lastModifedData, wsInfo)) {
216: wsInfo.setLastModifiedDate(fileItem.lastModified());
217: return true;
218: }
219: } else {
220: if (lastModifedData != fileItem.lastModified()) {
221: wsInfo.setLastModifiedDate(fileItem.lastModified());
222: return true;
223: }
224: }
225: }
226: return false;
227: }
228:
229: }
|