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 */
017package org.apache.wicket.resource;
018
019import java.io.IOException;
020import java.net.URI;
021import java.net.URISyntaxException;
022import java.nio.file.FileSystem;
023import java.nio.file.FileSystems;
024import java.nio.file.Path;
025import java.util.HashMap;
026import java.util.Map;
027
028import org.apache.wicket.Application;
029import org.apache.wicket.MetaDataKey;
030import org.apache.wicket.WicketRuntimeException;
031
032/**
033 * Gets the actual path for a jar file system
034 * 
035 * @author Tobias Soloschenko
036 *
037 */
038public class FileSystemJarPathService implements FileSystemPathService
039{
040
041        /** The key for the file system meta data **/
042        public static final MetaDataKey<Map<String, FileSystem>> FILE_SYSTEM_META_DATA_KEY = new MetaDataKey<>()
043        {
044                private static final long serialVersionUID = 1L;
045        };
046
047        @Override
048        public Path getPath(URI uri, Map<String, String> env)
049        {
050                try
051                {
052                        String uriString = uri.toString();
053                        int indexOfExclamationMark = uriString.indexOf('!');
054                        String jarFile = uriString.substring(0, indexOfExclamationMark);
055                        FileSystem fileSystem = null;
056
057                        synchronized (FILE_SYSTEM_META_DATA_KEY)
058                        {
059                                Map<String, FileSystem> metaData = Application.get()
060                                        .getMetaData(FILE_SYSTEM_META_DATA_KEY);
061                                if (metaData == null)
062                                {
063                                        metaData = new HashMap<String, FileSystem>();
064                                        Application.get().setMetaData(FILE_SYSTEM_META_DATA_KEY, metaData);
065                                }
066                                fileSystem = metaData.get(jarFile);
067                                if (fileSystem == null)
068                                {
069                                        if (env == null)
070                                        {
071                                                env = new HashMap<>();
072                                                env.put("create", "true");
073                                                env.put("encoding", "UTF-8");
074                                        }
075                                        fileSystem = FileSystems.newFileSystem(new URI(jarFile), env);
076                                        metaData.put(jarFile, fileSystem);
077                                }
078                        }
079                        String fileName = uriString.substring(uriString.indexOf('!') + 1);
080
081                        return fileSystem.getPath(fileName);
082                }
083                catch (IOException | URISyntaxException e)
084                {
085                        throw new WicketRuntimeException("Error while creating a jar file system", e);
086                }
087        }
088
089        @Override
090        public boolean isResponsible(URI uri)
091        {
092                return uri.getScheme().equals("jar") && uri.toString().indexOf('!') != -1;
093        }
094}