001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.pluto.util.install.file;
018:
019: import org.apache.commons.io.FileUtils;
020: import org.apache.pluto.util.install.InstallationConfig;
021: import org.apache.pluto.util.install.PortalInstaller;
022: import org.apache.pluto.util.UtilityException;
023:
024: import java.io.File;
025: import java.io.IOException;
026: import java.util.Iterator;
027: import java.util.Collection;
028:
029: /**
030: * File System based installer. Copies files to the appropriate
031: * locations.
032: *
033: */
034: public abstract class FileSystemInstaller implements PortalInstaller {
035:
036: protected void copyFilesToDirectory(Collection dependencies,
037: File destination) throws IOException {
038: Iterator it = dependencies.iterator();
039: while (it.hasNext()) {
040: File from = (File) it.next();
041: FileUtils.copyFileToDirectory(from, destination);
042: }
043: }
044:
045: protected void removeFilesFromDirectory(Collection dependencies,
046: File destination) throws IOException {
047: Iterator it = dependencies.iterator();
048: while (it.hasNext()) {
049: File from = (File) it.next();
050: File delete = new File(destination, from.getName());
051: delete.delete();
052: }
053: }
054:
055: protected void copyFileToDirectory(File file, File destination)
056: throws IOException {
057: FileUtils.copyFileToDirectory(file, destination);
058: }
059:
060: /**
061: * NOTE: Order is important. If the server is running, we want to
062: * make sure that the correct order is preserved
063: *
064: * 1) Install endorsed dependencies
065: * 2) Install shared dependencies
066: * 4) Prep Time
067: * -- Create a domain directory for the portal
068: * -- Init the configs holder
069: * 5) Install the Portlet Applications
070: * 6) Install the Portal Application
071: * 7) Finally, install the configs
072: * @param config
073: * @throws org.apache.pluto.util.UtilityException
074: */
075: public void install(InstallationConfig config)
076: throws UtilityException {
077: File endorsedDir = getEndorsedDir(config);
078: File sharedDir = getSharedDir(config);
079: File domainDir = getWebAppDir(config);
080:
081: endorsedDir.mkdirs();
082: sharedDir.mkdirs();
083: domainDir.mkdirs();
084:
085: try {
086: copyFilesToDirectory(config.getEndorsedDependencies(),
087: endorsedDir);
088: copyFilesToDirectory(config.getSharedDependencies(),
089: sharedDir);
090:
091: copyFilesToDirectory(config.getPortletApplications()
092: .values(), domainDir);
093: copyFileToDirectory(config.getPortalApplication(),
094: domainDir);
095:
096: writeConfiguration(config);
097: } catch (IOException io) {
098: throw new UtilityException(
099: "Unable to install portal to Tomcat", io, config
100: .getInstallationDirectory());
101: }
102: }
103:
104: public abstract void writeConfiguration(InstallationConfig config)
105: throws IOException;
106:
107: protected abstract File getEndorsedDir(InstallationConfig config);
108:
109: protected abstract File getSharedDir(InstallationConfig config);
110:
111: protected abstract File getWebAppDir(InstallationConfig config);
112:
113: public void uninstall(InstallationConfig config)
114: throws UtilityException {
115: File endorsedDir = getEndorsedDir(config);
116: File sharedDir = getSharedDir(config);
117: File domainDir = getWebAppDir(config);
118:
119: endorsedDir.mkdirs();
120: sharedDir.mkdirs();
121: domainDir.mkdirs();
122:
123: try {
124: removeFilesFromDirectory(config.getEndorsedDependencies(),
125: endorsedDir);
126: removeFilesFromDirectory(config.getSharedDependencies(),
127: sharedDir);
128: removeFilesFromDirectory(config.getPortletApplications()
129: .values(), domainDir);
130:
131: File delete = new File(domainDir, config
132: .getPortalApplication().getName());
133: delete.delete();
134: } catch (IOException io) {
135: throw new UtilityException("Unable to remove files. ", io,
136: config.getInstallationDirectory());
137: }
138:
139: }
140:
141: public abstract boolean isValidInstallationDirectory(File installDir);
142: }
|