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.geronimo.system.repository;
018    
019    import java.io.File;
020    import java.net.URI;
021    import java.net.URISyntaxException;
022    
023    import org.apache.geronimo.system.serverinfo.ServerInfo;
024    
025    /**
026     * @version $Rev: 476049 $ $Date: 2006-11-17 15:35:17 +1100 (Fri, 17 Nov 2006) $
027     */
028    public class ServerInfoRootResolver {
029        private final ServerInfo serverInfo;
030        private final boolean resolveToServer;
031    
032        public ServerInfoRootResolver(ServerInfo serverInfo, boolean resolveToServer) {
033            this.serverInfo = serverInfo;
034            this.resolveToServer = resolveToServer;
035        }
036        
037        public File resolve(URI root) {
038            if (root == null) throw new NullPointerException("root is null");
039    
040            if (!root.toString().endsWith("/")) {
041                try {
042                    root = new URI(root.toString() + "/");
043                } catch (URISyntaxException e) {
044                    throw new RuntimeException("Invalid repository root (does not end with / ) and can't add myself", e);
045                }
046            }
047    
048            URI resolvedUri;
049            if (serverInfo != null) {
050                resolvedUri = resolveToServer ? serverInfo.resolveServer(root) : serverInfo.resolve(root);
051            } else {
052                resolvedUri = root;
053            }
054    
055            if (!resolvedUri.getScheme().equals("file")) {
056                throw new IllegalStateException("FileSystemRepository must have a root that's a local directory (not " + resolvedUri + ")");
057            }
058    
059            File rootFile = new File(resolvedUri);
060            return rootFile;
061        }
062    
063    }