1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20
21 package org.apache.axis2.deployment.repository.util;
22
23 import org.apache.axis2.deployment.Deployer;
24 import org.apache.axis2.deployment.DeploymentConstants;
25 import org.apache.axis2.deployment.DeploymentEngine;
26
27 import java.io.File;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Map;
33
34 public class WSInfoList implements DeploymentConstants {
35
36 /**
37 * This is to store all the jar files in a specified folder (WEB_INF)
38 */
39 private List jarList = new ArrayList();
40
41 /**
42 * All the currently updated jars
43 */
44 public Map currentJars = new HashMap();
45
46 /**
47 * Reference to DeploymentEngine to make update
48 */
49
50 private boolean locked = false;
51 private final DeploymentEngine deploymentEngine;
52
53 public WSInfoList(DeploymentEngine deploy_engine) {
54 deploymentEngine = deploy_engine;
55 }
56
57 /**
58 * First checks whether the file is already available by the
59 * system call fileExists. If it is not deployed yet then adds to the jarList
60 * and to the deployment engine as a new service or module.
61 * While adding new item to jarList, first creates the WSInfo object and
62 * then adds to the jarlist and actual jar file is added to DeploymentEngine.
63 * <p/>
64 * If the files already exists, then checks whether it has been updated
65 * then changes the last update date of the wsInfo and adds two entries to
66 * DeploymentEngine - one for new deployment and other for undeployment.
67 *
68 * @param file actual jar files for either Module or service
69 */
70 public synchronized void addWSInfoItem(File file, Deployer deployer , int type) {
71 WSInfo info = (WSInfo) currentJars.get(file.getAbsolutePath());
72 if (info != null) {
73 if (deploymentEngine.isHotUpdate() && isModified(file, info)) {
74 WSInfo wsInfo = new WSInfo(info.getFileName(), info.getLastModifiedDate(), deployer,type);
75 deploymentEngine.addWSToUndeploy(wsInfo); // add entry to undeploy list
76 DeploymentFileData deploymentFileData = new DeploymentFileData(file, deployer);
77 deploymentEngine.addWSToDeploy(deploymentFileData); // add entry to deploylist
78 }
79 } else {
80 info = getFileItem(file, deployer, type);
81 setLastModifiedDate(file, info);
82 }
83
84 jarList.add(info.getFileName());
85 }
86
87 /**
88 * Checks undeployed Services. Checks old jars files and current jars.
89 * If name of the old jar file does not exist in the current jar
90 * list then it is assumed that the jar file has been removed
91 * and that is hot undeployment.
92 */
93 private synchronized void checkForUndeployedServices() {
94 if(!locked) {
95 locked = true;
96 } else{
97 return;
98 }
99 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.getAbsolutePath(), file.lastModified(), deployer ,type);
157 currentJars.put(file.getAbsolutePath(), info);
158 DeploymentFileData fileData = new DeploymentFileData(file, deployer);
159 deploymentEngine.addWSToDeploy(fileData);
160 }
161 return info;
162 }
163
164 /**
165 * Checks if a file has been modified by comparing the last update date of
166 * both files and WSInfo. If they are different, the file is assumed to have
167 * been modified.
168 *
169 * @param file
170 * @param wsInfo
171 */
172 private boolean isModified(File file, WSInfo wsInfo) {
173 long currentTimeStamp = wsInfo.getLastModifiedDate();
174
175 setLastModifiedDate(file, wsInfo);
176
177 return (currentTimeStamp != wsInfo.getLastModifiedDate());
178 }
179
180 /**
181 * Obtains the newest (as compared with timestamp stored in wsInfo)
182 * timestamp and stores it in WSInfo.
183 */
184 private void setLastModifiedDate(File file, WSInfo wsInfo) {
185 if (file.isDirectory()) {
186 File files [] = file.listFiles();
187 for (int i = 0; i < files.length; i++) {
188 File fileItem = files[i];
189 if (fileItem.isDirectory()) {
190 setLastModifiedDate(fileItem, wsInfo);
191 }
192 else if(wsInfo.getLastModifiedDate() < fileItem.lastModified()) {
193 wsInfo.setLastModifiedDate(fileItem.lastModified());
194 }
195 }
196 }
197 else if(wsInfo.getLastModifiedDate() < file.lastModified()) {
198 wsInfo.setLastModifiedDate(file.lastModified());
199 }
200 }
201 }