Source Code Cross Referenced for GeoServerResourceLoader.java in  » GIS » GeoServer » org » geoserver » platform » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » GIS » GeoServer » org.geoserver.platform 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002:         * This code is licensed under the GPL 2.0 license, availible at the root
003:         * application directory.
004:         */
005:        package org.geoserver.platform;
006:
007:        import org.springframework.core.io.DefaultResourceLoader;
008:        import org.springframework.core.io.Resource;
009:        import java.io.File;
010:        import java.io.IOException;
011:        import java.util.Collections;
012:        import java.util.HashSet;
013:        import java.util.Iterator;
014:        import java.util.Set;
015:        import java.util.TreeSet;
016:        import java.util.logging.Logger;
017:
018:        /**
019:         * Manages resources in GeoServer.
020:         * <p>
021:         * The loader maintains a search path in which it will use to look up resources.
022:         * The {@link #baseDirectory} is a member of this path.
023:         * </p>
024:         * <p>
025:         * Files and directories created by the resource loader are made relative to
026:         * {@link #baseDirectory}.
027:         * </p>
028:         * <p>
029:         * <pre>
030:         *         <code>
031:         * File dataDirectory = ...
032:         * GeoServerResourceLoader loader = new GeoServerResourceLoader( dataDirectory );
033:         * loader.addSearchLocation( new File( "/WEB-INF/" ) );
034:         * loader.addSearchLocation( new File( "/data" ) );
035:         * ...
036:         * File catalog = loader.find( "catalog.xml" );
037:         *         </code>
038:         * </pre>
039:         * </p>
040:         *
041:         * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
042:         *
043:         */
044:        public class GeoServerResourceLoader extends DefaultResourceLoader {
045:            private static final Logger LOGGER = org.geotools.util.logging.Logging
046:                    .getLogger("org.vfny.geoserver.global");
047:
048:            /** "path" for resource lookups */
049:            Set searchLocations;
050:
051:            /**
052:             * Base directory
053:             */
054:            File baseDirectory;
055:
056:            /**
057:             * Creates a new resource loader with no base directory.
058:             * <p>
059:             * Such a constructed resource loader is not capable of creating resources
060:             * from relative paths.
061:             * </p>
062:             */
063:            public GeoServerResourceLoader() {
064:                searchLocations = new TreeSet();
065:            }
066:
067:            /**
068:             * Creates a new resource loader.
069:             *
070:             * @param baseDirectory The directory in which
071:             */
072:            public GeoServerResourceLoader(File baseDirectory) {
073:                this ();
074:                this .baseDirectory = baseDirectory;
075:                setSearchLocations(Collections.EMPTY_SET);
076:            }
077:
078:            /**
079:             * Adds a location to the path used for resource lookups.
080:             *
081:             * @param A directory containing resources.
082:             */
083:            public void addSearchLocation(File searchLocation) {
084:                searchLocations.add(searchLocation);
085:            }
086:
087:            /**
088:             * Sets the search locations used for resource lookups.
089:             *
090:             * @param searchLocations A set of {@link File}.
091:             */
092:            public void setSearchLocations(Set searchLocations) {
093:                this .searchLocations = new HashSet(searchLocations);
094:
095:                //always add the base directory
096:                if (baseDirectory != null) {
097:                    this .searchLocations.add(baseDirectory);
098:                }
099:            }
100:
101:            /**
102:             * @return The base directory.
103:             */
104:            public File getBaseDirectory() {
105:                return baseDirectory;
106:            }
107:
108:            /**
109:             * Sets the base directory.
110:             *
111:             * @param baseDirectory
112:             */
113:            public void setBaseDirectory(File baseDirectory) {
114:                this .baseDirectory = baseDirectory;
115:
116:                searchLocations.add(baseDirectory);
117:            }
118:
119:            /**
120:             * Performs a resource lookup.
121:             *
122:             * @param location The name of the resource to lookup, can be absolute or
123:             * relative.
124:             *
125:             * @return The file handle representing the resource, or null if the
126:             * resource could not be found.
127:             *
128:             * @throws IOException In the event of an I/O error.
129:             */
130:            public File find(String location) throws IOException {
131:                //first to an existance check
132:                File file = new File(location);
133:
134:                if (file.isAbsolute()) {
135:                    return file;
136:                } else {
137:                    //try a relative url
138:                    for (Iterator f = searchLocations.iterator(); f.hasNext();) {
139:                        File base = (File) f.next();
140:                        file = new File(base, location);
141:
142:                        try {
143:                            if (file.exists()) {
144:                                return file;
145:                            }
146:                        } catch (SecurityException e) {
147:                            LOGGER
148:                                    .warning("Failed attemp to check existance of "
149:                                            + file.getAbsolutePath());
150:                        }
151:                    }
152:                }
153:
154:                Resource resource = getResource(location);
155:
156:                if (resource.exists()) {
157:                    return resource.getFile();
158:                }
159:
160:                return null;
161:            }
162:
163:            /**
164:             * Creates a new directory.
165:             * <p>
166:             * Relative paths are created relative to {@link #baseDirectory}.
167:             * If {@link #baseDirectory} is not set, an IOException is thrown.
168:             * </p>
169:             * <p>
170:             * If <code>location</code> already exists as a file, an IOException is thrown.
171:             * </p>
172:             * @param location Location of directory to create, either absolute or
173:             * relative.
174:             *
175:             * @return The file handle of the created directory.
176:             *
177:             * @throws IOException
178:             */
179:            public File createDirectory(String location) throws IOException {
180:                File file = find(location);
181:
182:                if (file != null) {
183:                    if (!file.isDirectory()) {
184:                        String msg = location
185:                                + " already exists and is not directory";
186:                        throw new IOException(msg);
187:                    }
188:                }
189:
190:                file = new File(location);
191:
192:                if (file.isAbsolute()) {
193:                    file.mkdir();
194:
195:                    return file;
196:                }
197:
198:                //no base directory set, cannot create a relative path
199:                if (baseDirectory == null) {
200:                    String msg = "No base location set, could not create directory: "
201:                            + location;
202:                    throw new IOException(msg);
203:                }
204:
205:                file = new File(baseDirectory, location);
206:                file.mkdir();
207:
208:                return file;
209:            }
210:
211:            /**
212:             * Creates a new file.
213:             * <p>
214:             * Relative paths are created relative to {@link #baseDirectory}.
215:             * </p>
216:             * If {@link #baseDirectory} is not set, an IOException is thrown.
217:             * </p>
218:             * <p>
219:             * If <code>location</code> already exists as a directory, an IOException is thrown.
220:             * </p>
221:             * @param location Location of file to create, either absolute or relative.
222:             *
223:             * @return The file handle of the created file.
224:             *
225:             * @throws IOException In the event of an I/O error.
226:             */
227:            public File createFile(String location) throws IOException {
228:                File file = find(location);
229:
230:                if (file != null) {
231:                    if (file.isDirectory()) {
232:                        String msg = location
233:                                + " already exists and is a directory";
234:                        throw new IOException(msg);
235:                    }
236:
237:                    return file;
238:                }
239:
240:                file = new File(location);
241:
242:                if (file.isAbsolute()) {
243:                    file.createNewFile();
244:
245:                    return file;
246:                }
247:
248:                //no base directory set, cannot create a relative path
249:                if (baseDirectory == null) {
250:                    String msg = "No base location set, could not create file: "
251:                            + location;
252:                    throw new IOException(msg);
253:                }
254:
255:                file = new File(baseDirectory, location);
256:                file.createNewFile();
257:
258:                return file;
259:            }
260:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.