001/*
002 * (C) Copyright 2006-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 *     jcarsique
019 *     Yannis JULIENNE
020 */
021package org.nuxeo.connect.update.standalone;
022
023import static java.nio.charset.StandardCharsets.UTF_8;
024
025import java.io.File;
026import java.io.IOException;
027import java.nio.file.Files;
028import java.nio.file.Path;
029import java.nio.file.attribute.BasicFileAttributes;
030import java.nio.file.attribute.FileTime;
031import java.security.SecureRandom;
032import java.util.ArrayList;
033import java.util.HashMap;
034import java.util.List;
035import java.util.Map;
036import java.util.Map.Entry;
037import java.util.Random;
038
039import org.apache.commons.io.FileUtils;
040import org.apache.commons.logging.Log;
041import org.apache.commons.logging.LogFactory;
042import org.nuxeo.common.Environment;
043import org.nuxeo.common.utils.ZipUtils;
044import org.nuxeo.connect.update.AlreadyExistsPackageException;
045import org.nuxeo.connect.update.LocalPackage;
046import org.nuxeo.connect.update.PackageException;
047import org.nuxeo.connect.update.PackageState;
048import org.nuxeo.connect.update.PackageUpdateService;
049
050/**
051 * The file {@code nxserver/data/packages/.packages} stores the state of all local features.
052 * <p>
053 * Each local package have a corresponding directory in {@code nxserver/data/features/store} which is named:
054 * {@code <package_uid>} ("id-version")
055 *
056 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
057 */
058public class PackagePersistence {
059
060    private static final Log log = LogFactory.getLog(PackagePersistence.class);
061
062    protected final File root;
063
064    protected final File store;
065
066    protected final File temp;
067
068    protected static final Random RANDOM = new SecureRandom();
069
070    protected Map<String, PackageState> states;
071
072    private PackageUpdateService service;
073
074    public PackagePersistence(PackageUpdateService pus) throws IOException {
075        Environment env = Environment.getDefault();
076        root = env.getPath(Environment.NUXEO_MP_DIR, Environment.DEFAULT_MP_DIR);
077        if (!root.isAbsolute()) {
078            throw new RuntimeException();
079        }
080        root.mkdirs();
081        store = new File(root, "store");
082        store.mkdirs();
083        temp = new File(root, "tmp");
084        temp.mkdirs();
085        service = pus;
086        states = loadStates();
087    }
088
089    public File getRoot() {
090        return root;
091    }
092
093    /**
094     * @since 7.1
095     */
096    public File getStore() {
097        return store;
098    }
099
100    public synchronized Map<String, PackageState> getStates() {
101        return new HashMap<>(states);
102    }
103
104    protected Map<String, PackageState> loadStates() throws IOException {
105        Map<String, PackageState> result = new HashMap<>();
106        File file = new File(root, ".packages");
107        if (file.isFile()) {
108            List<String> lines = FileUtils.readLines(file, UTF_8);
109            for (String line : lines) {
110                line = line.trim();
111                if (line.length() == 0 || line.startsWith("#")) {
112                    continue;
113                }
114                int i = line.indexOf('=');
115                String pkgId = line.substring(0, i).trim();
116                String value = line.substring(i + 1).trim();
117                PackageState state = PackageState.getByLabel(value);
118                if (state == PackageState.UNKNOWN) {
119                    try {
120                        // Kept for backward compliance with int instead of enum
121                        state = PackageState.getByValue(value);
122                    } catch (NumberFormatException e) {
123                        // Set as REMOTE if undefined/unreadable
124                        state = PackageState.REMOTE;
125                    }
126                }
127                result.put(pkgId, state);
128            }
129        }
130        return result;
131    }
132
133    protected void writeStates() throws IOException {
134        StringBuilder sb = new StringBuilder();
135        for (Entry<String, PackageState> entry : states.entrySet()) {
136            sb.append(entry.getKey()).append('=').append(entry.getValue()).append("\n");
137        }
138        File file = new File(root, ".packages");
139        FileUtils.writeStringToFile(file, sb.toString(), UTF_8);
140    }
141
142    public LocalPackage getPackage(String id) throws PackageException {
143        File file = new File(store, id);
144        if (file.isDirectory()) {
145            return new LocalPackageImpl(file, getState(id), service);
146        }
147        return null;
148    }
149
150    public synchronized LocalPackage addPackage(File file) throws PackageException {
151        if (file.isDirectory()) {
152            return addPackageFromDir(file);
153        } else if (file.isFile()) {
154            File tmp = newTempDir(file.getName());
155            try {
156                ZipUtils.unzip(file, tmp);
157                return addPackageFromDir(tmp);
158            } catch (IOException e) {
159                throw new PackageException("Failed to unzip package: " + file.getName());
160            } finally {
161                // cleanup tmp if exists
162                org.apache.commons.io.FileUtils.deleteQuietly(tmp);
163            }
164        } else {
165            throw new PackageException("Not found: " + file);
166        }
167    }
168
169    /**
170     * Add unzipped packaged to local cache. It replaces SNAPSHOT packages if not installed
171     *
172     * @throws AlreadyExistsPackageException If not replacing a SNAPSHOT or if the existing package is installed
173     */
174    protected LocalPackage addPackageFromDir(File file) throws PackageException {
175        LocalPackageImpl pkg = new LocalPackageImpl(file, PackageState.DOWNLOADED, service);
176        File dir = null;
177        try {
178            dir = new File(store, pkg.getId());
179            if (dir.exists()) {
180                LocalPackage oldpkg = getPackage(pkg.getId());
181                if (!pkg.getVersion().isSnapshot()) {
182                    throw new AlreadyExistsPackageException("Package " + pkg.getId() + " already exists");
183                }
184                if (oldpkg.getPackageState().isInstalled()) {
185                    throw new AlreadyExistsPackageException("Package " + pkg.getId() + " is already installed");
186                }
187                log.info(String.format("Replacement of %s in local cache...", oldpkg));
188                org.apache.commons.io.FileUtils.deleteQuietly(dir);
189            }
190            org.apache.commons.io.FileUtils.copyDirectory(file, dir);
191            pkg.getData().setRoot(dir);
192            updateState(pkg.getId(), pkg.state);
193            return pkg;
194        } catch (IOException e) {
195            throw new PackageException(String.format("Failed to move %s to %s", file, dir), e);
196        }
197    }
198
199    public synchronized PackageState getState(String packageId) {
200        PackageState state = states.get(packageId);
201        if (state == null) {
202            return PackageState.REMOTE;
203        }
204        return state;
205    }
206
207    /**
208     * Get the local package having the given name and which is in either one of the following states:
209     * <ul>
210     * <li>{@link PackageState#INSTALLING}
211     * <li>{@link PackageState#INSTALLED}
212     * <li>{@link PackageState#STARTED}
213     * </ul>
214     */
215    public LocalPackage getActivePackage(String name) throws PackageException {
216        String pkgId = getActivePackageId(name);
217        if (pkgId == null) {
218            return null;
219        }
220        return getPackage(pkgId);
221    }
222
223    public synchronized String getActivePackageId(String name) throws PackageException {
224        for (Entry<String, PackageState> entry : states.entrySet()) {
225            String pkgId = entry.getKey();
226            if (pkgId.startsWith(name) && entry.getValue().isInstalled() && getPackage(pkgId).getName().equals(name)) {
227                return pkgId;
228            }
229        }
230        return null;
231    }
232
233    public synchronized List<LocalPackage> getPackages() throws PackageException {
234        File[] list = store.listFiles();
235        if (list != null) {
236            List<LocalPackage> pkgs = new ArrayList<>(list.length);
237            for (File file : list) {
238                if (!file.isDirectory()) {
239                    log.warn("Ignoring file '" + file.getName() + "' in package store");
240                    continue;
241                }
242                pkgs.add(new LocalPackageImpl(file, getState(file.getName()), service));
243            }
244            return pkgs;
245        }
246        return new ArrayList<>();
247    }
248
249    public synchronized void removePackage(String id) throws PackageException {
250        states.remove(id);
251        try {
252            writeStates();
253        } catch (IOException e) {
254            throw new PackageException("Failed to write package states", e);
255        }
256        File file = new File(store, id);
257        org.apache.commons.io.FileUtils.deleteQuietly(file);
258    }
259
260    /**
261     * @deprecated Since 5.7. Use {@link #updateState(String, PackageState)} instead.
262     */
263    @Deprecated
264    public synchronized void updateState(String id, int state) throws PackageException {
265        states.put(id, PackageState.getByValue(state));
266        try {
267            writeStates();
268        } catch (IOException e) {
269            throw new PackageException("Failed to write package states", e);
270        }
271    }
272
273    /**
274     * @since 5.7
275     */
276    public synchronized void updateState(String id, PackageState state) throws PackageException {
277        states.put(id, state);
278        try {
279            writeStates();
280        } catch (IOException e) {
281            throw new PackageException("Failed to write package states", e);
282        }
283    }
284
285    public synchronized void reset() throws PackageException {
286        String[] keys = states.keySet().toArray(new String[states.size()]);
287        for (String key : keys) {
288            states.put(key, PackageState.DOWNLOADED);
289        }
290        try {
291            writeStates();
292        } catch (IOException e) {
293            throw new PackageException("Failed to write package states", e);
294        }
295    }
296
297    protected File newTempDir(String id) {
298        File tmp;
299        synchronized (temp) {
300            do {
301                tmp = new File(temp, id + "-" + RANDOM.nextInt());
302            } while (tmp.exists());
303            tmp.mkdirs();
304        }
305        return tmp;
306    }
307
308    /**
309     * @since 5.8
310     */
311    public FileTime getInstallDate(String id) {
312        File file = new File(store, id);
313        if (file.isDirectory()) {
314            Path path = file.toPath();
315            try {
316                FileTime lastModifiedTime = Files.readAttributes(path, BasicFileAttributes.class).lastModifiedTime();
317                return lastModifiedTime;
318            } catch (IOException e) {
319                log.error(e);
320            }
321        }
322        return null;
323    }
324}