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.netty.worker;
016    
017    import com.liferay.portal.kernel.io.PathHolder;
018    import com.liferay.portal.kernel.process.ProcessCallable;
019    import com.liferay.portal.kernel.process.ProcessConfig;
020    import com.liferay.portal.kernel.process.ProcessException;
021    import com.liferay.util.SerializableUtil;
022    
023    import java.io.Serializable;
024    
025    import java.nio.file.Path;
026    
027    import java.util.HashMap;
028    import java.util.Map;
029    
030    /**
031     * @author Shuyang Zhou
032     */
033    public class NettyFabricWorkerConfig<T extends Serializable>
034            implements Serializable {
035    
036            public NettyFabricWorkerConfig(
037                    long id, ProcessConfig processConfig,
038                    ProcessCallable<T> processCallable, Map<Path, Path> inputPathMap) {
039    
040                    if (processConfig == null) {
041                            throw new NullPointerException("Process config is null");
042                    }
043    
044                    if (processCallable == null) {
045                            throw new NullPointerException("Process callable is null");
046                    }
047    
048                    if (inputPathMap == null) {
049                            throw new NullPointerException("Input path map is null");
050                    }
051    
052                    _id = id;
053                    _processConfig = processConfig;
054                    _processCallable = new NettyFabricWorkerProcessCallable<>(
055                            processCallable);
056    
057                    _inputPathHolderMap = new HashMap<>();
058    
059                    for (Map.Entry<Path, Path> entry : inputPathMap.entrySet()) {
060                            _inputPathHolderMap.put(
061                                    new PathHolder(entry.getKey()),
062                                    new PathHolder(entry.getValue()));
063                    }
064            }
065    
066            public long getId() {
067                    return _id;
068            }
069    
070            public Map<Path, Path> getInputPathMap() {
071                    Map<Path, Path> inputPathMap = new HashMap<>();
072    
073                    for (Map.Entry<PathHolder, PathHolder> entry :
074                                    _inputPathHolderMap.entrySet()) {
075    
076                            PathHolder keyPathHolder = entry.getKey();
077                            PathHolder valuePathHolder = entry.getValue();
078    
079                            inputPathMap.put(
080                                    keyPathHolder.getPath(), valuePathHolder.getPath());
081                    }
082    
083                    return inputPathMap;
084            }
085    
086            public ProcessCallable<T> getProcessCallable() {
087                    return _processCallable;
088            }
089    
090            public ProcessConfig getProcessConfig() {
091                    return _processConfig;
092            }
093    
094            private static final long serialVersionUID = 1L;
095    
096            private final long _id;
097            private final Map<PathHolder, PathHolder> _inputPathHolderMap;
098            private final ProcessCallable<T> _processCallable;
099            private final ProcessConfig _processConfig;
100    
101            private static class NettyFabricWorkerProcessCallable
102                    <T extends Serializable>
103                            implements ProcessCallable<T> {
104    
105                    public NettyFabricWorkerProcessCallable(
106                            ProcessCallable<T> processCallable) {
107    
108                            _data = SerializableUtil.serialize(processCallable);
109                            _toString = processCallable.toString();
110                    }
111    
112                    @Override
113                    public T call() throws ProcessException {
114                            ProcessCallable<T> processCallable =
115                                    (ProcessCallable<T>)SerializableUtil.deserialize(_data);
116    
117                            return processCallable.call();
118                    }
119    
120                    @Override
121                    public String toString() {
122                            return _toString;
123                    }
124    
125                    private static final long serialVersionUID = 1L;
126    
127                    private final byte[] _data;
128                    private final String _toString;
129    
130            }
131    
132    }