KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > services > deployment > LibraryManager


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.services.deployment;
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.net.URL JavaDoc;
27
28 import org.jboss.logging.Logger;
29 import org.jboss.system.server.ServerConfig;
30 import org.jboss.system.server.ServerConfigLocator;
31 import org.jboss.util.file.Files;
32
33 /**
34  * Simple helper singleton to manage library operations
35  *
36  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
37  * @version $Revision: 37459 $
38  */

39 public final class LibraryManager
40 {
41    // Static --------------------------------------------------------
42

43    /** the Logger instance */
44    private static final Logger log = Logger.getLogger(LibraryManager.class);
45    
46    /** the singleton instance */
47    private static final LibraryManager INSTANCE = new LibraryManager();
48    
49    // Private Data --------------------------------------------------
50

51    /** The local server library dir */
52    File JavaDoc serverLibDir;
53    
54    /** The local server tmp dir */
55    File JavaDoc serverTmpDir;
56    
57    // Constructors --------------------------------------------------
58

59    /**
60     * Private CTOR
61     *
62     * Requires that ServerConfig object is created
63     * and registered to the jboss MBeanServer
64     */

65    private LibraryManager()
66    {
67       // discover if there is a local server library dir
68
ServerConfig config = ServerConfigLocator.locate();
69       URL JavaDoc serverLibURL = config.getServerLibraryURL();
70       
71       if (serverLibURL != null && serverLibURL.getProtocol().startsWith("file"))
72       {
73          this.serverLibDir = new File JavaDoc(serverLibURL.getFile());
74          this.serverTmpDir = config.getServerTempDir();
75          log.debug("Using serverLibDir: " + this.serverLibDir);
76          log.debug("Using serverTmpDir: " + this.serverTmpDir);
77       }
78       else
79       {
80          log.info("Cannot manage remote serverLibraryURL: " + serverLibURL);
81       }
82    }
83    
84    // Public --------------------------------------------------------
85

86    /**
87     * Gets the singleton
88     */

89    public static LibraryManager getInstance()
90    {
91       return INSTANCE;
92    }
93    
94    /**
95     * Upload a new library to server lib dir. A different
96     * filename may be specified, when writing the library.
97     *
98     * If the target filename exists, upload is not performed.
99     *
100     * @param src the source url to copy
101     * @param filename the filename to use when copying (optional)
102     * @return true if upload was succesful, false otherwise
103     */

104    public boolean uploadLibrary(URL JavaDoc src, String JavaDoc filename)
105    {
106       if (src != null)
107       {
108          log.debug("Uploading from URL: " + src);
109          if (filename == null || filename.equals(""))
110          {
111             // get the basename of the url, let File do the dirty work
112
filename = (new File JavaDoc(src.getPath())).getName();
113             log.debug("Null or empty target filename, using basename: " + filename);
114          }
115          else
116          {
117             log.debug("Using target filename: " + filename);
118          }
119          // make sure target file does not exist
120
File JavaDoc target = new File JavaDoc(this.serverLibDir, filename);
121          if (!target.exists())
122          {
123             try
124             {
125                Files.copy(src, target);
126                return true; // success
127
}
128             catch (IOException JavaDoc e)
129             {
130                log.warn("Could not upload target library: " + filename, e);
131             }
132          }
133          else
134          {
135             log.warn("Target library exists: " + filename);
136          }
137       }
138       else
139       {
140          log.warn("Null src URL");
141       }
142       // upload failed
143
return false;
144    }
145 }
146
Popular Tags