001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.services.deployment;
023:
024: import java.io.File;
025: import java.io.IOException;
026: import java.net.URL;
027:
028: import org.jboss.logging.Logger;
029: import org.jboss.system.server.ServerConfig;
030: import org.jboss.system.server.ServerConfigLocator;
031: import org.jboss.util.file.Files;
032:
033: /**
034: * Simple helper singleton to manage library operations
035: *
036: * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
037: * @version $Revision: 57210 $
038: */
039: public final class LibraryManager {
040: // Static --------------------------------------------------------
041:
042: /** the Logger instance */
043: private static final Logger log = Logger
044: .getLogger(LibraryManager.class);
045:
046: /** the singleton instance */
047: private static final LibraryManager INSTANCE = new LibraryManager();
048:
049: // Private Data --------------------------------------------------
050:
051: /** The local server library dir */
052: File serverLibDir;
053:
054: /** The local server tmp dir */
055: File serverTmpDir;
056:
057: // Constructors --------------------------------------------------
058:
059: /**
060: * Private CTOR
061: *
062: * Requires that ServerConfig object is created
063: * and registered to the jboss MBeanServer
064: */
065: private LibraryManager() {
066: // discover if there is a local server library dir
067: ServerConfig config = ServerConfigLocator.locate();
068: URL serverLibURL = config.getServerLibraryURL();
069:
070: if (serverLibURL != null
071: && serverLibURL.getProtocol().startsWith("file")) {
072: this .serverLibDir = new File(serverLibURL.getFile());
073: this .serverTmpDir = config.getServerTempDir();
074: log.debug("Using serverLibDir: " + this .serverLibDir);
075: log.debug("Using serverTmpDir: " + this .serverTmpDir);
076: } else {
077: log.info("Cannot manage remote serverLibraryURL: "
078: + serverLibURL);
079: }
080: }
081:
082: // Public --------------------------------------------------------
083:
084: /**
085: * Gets the singleton
086: */
087: public static LibraryManager getInstance() {
088: return INSTANCE;
089: }
090:
091: /**
092: * Upload a new library to server lib dir. A different
093: * filename may be specified, when writing the library.
094: *
095: * If the target filename exists, upload is not performed.
096: *
097: * @param src the source url to copy
098: * @param filename the filename to use when copying (optional)
099: * @return true if upload was succesful, false otherwise
100: */
101: public boolean uploadLibrary(URL src, String filename) {
102: if (src != null) {
103: log.debug("Uploading from URL: " + src);
104: if (filename == null || filename.equals("")) {
105: // get the basename of the url, let File do the dirty work
106: filename = (new File(src.getPath())).getName();
107: log
108: .debug("Null or empty target filename, using basename: "
109: + filename);
110: } else {
111: log.debug("Using target filename: " + filename);
112: }
113: // make sure target file does not exist
114: File target = new File(this .serverLibDir, filename);
115: if (!target.exists()) {
116: try {
117: Files.copy(src, target);
118: return true; // success
119: } catch (IOException e) {
120: log.warn("Could not upload target library: "
121: + filename, e);
122: }
123: } else {
124: log.warn("Target library exists: " + filename);
125: }
126: } else {
127: log.warn("Null src URL");
128: }
129: // upload failed
130: return false;
131: }
132: }
|