001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.fabric;
016    
017    import com.liferay.portal.fabric.repository.RepositoryHelperUtil;
018    import com.liferay.portal.kernel.process.ProcessCallable;
019    import com.liferay.portal.kernel.util.ObjectGraphUtil.AnnotatedFieldMappingVisitor;
020    
021    import java.io.File;
022    
023    import java.lang.annotation.Annotation;
024    import java.lang.reflect.Field;
025    
026    import java.nio.file.Path;
027    
028    import java.util.Collections;
029    import java.util.HashMap;
030    import java.util.Map;
031    
032    /**
033     * @author Shuyang Zhou
034     */
035    public class FabricResourceMappingVisitor extends AnnotatedFieldMappingVisitor {
036    
037            public FabricResourceMappingVisitor(
038                    Class<? extends Annotation> annotationClass,
039                    Path remoteRepositoryPath) {
040    
041                    super(
042                            Collections.<Class<?>>singleton(ProcessCallable.class),
043                            Collections.<Class<? extends Annotation>>singleton(annotationClass),
044                            Collections.<Class<?>>singleton(File.class));
045    
046                    _remoteRepositoryPath = remoteRepositoryPath;
047            }
048    
049            public Map<Path, Path> getResourceMap() {
050                    return _resourceMap;
051            }
052    
053            @Override
054            protected Object doMap(Field field, Object value) {
055                    File file = (File)value;
056    
057                    Path path = file.toPath();
058    
059                    Path mappedPath = RepositoryHelperUtil.getRepositoryFilePath(
060                            _remoteRepositoryPath, path);
061    
062                    mappedPath = mappedPath.toAbsolutePath();
063    
064                    _resourceMap.put(path.toAbsolutePath(), mappedPath);
065    
066                    return mappedPath.toFile();
067            }
068    
069            private final Path _remoteRepositoryPath;
070            private final Map<Path, Path> _resourceMap = new HashMap<Path, Path>();
071    
072    }