Clover icon

Magnolia Content Importer Module 1.0

  1. Project Clover database Thu Nov 10 2016 15:11:38 CET
  2. Package info.magnolia.content.observer

File ContentImporterModule.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
66% of files have more coverage

Code metrics

2
11
4
2
108
52
6
0.55
2.75
2
1.5

Classes

Class Line # Actions
ContentImporterModule 60 10 0% 5 0
1.0100%
ContentImporterModule.AlwaysTruePredicate 102 1 0% 1 2
0.00%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /**
2    * This file Copyright (c) 2016 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.content.observer;
35   
36    import info.magnolia.dirwatch.DirectoryWatcherService;
37    import info.magnolia.init.MagnoliaConfigurationProperties;
38    import info.magnolia.module.ModuleLifecycle;
39    import info.magnolia.module.ModuleLifecycleContext;
40   
41    import java.io.IOException;
42    import java.nio.file.Path;
43    import java.nio.file.Paths;
44   
45    import javax.inject.Inject;
46   
47    import org.apache.commons.lang3.StringUtils;
48    import org.slf4j.Logger;
49    import org.slf4j.LoggerFactory;
50   
51    import com.google.common.base.Predicate;
52   
53    /**
54    * Responsible for observation of bootstrap content folder which should be specified under magnolia.properties file.
55    * <p>
56    * Only starts to observe if {@link #CONTENT_BOOTSTRAP_DIR_PROPERTY_KEY dedicated property key} is specified.
57    * Filters out all the files except the ones with 'XML' extension.
58    * </p>
59    */
 
60    public class ContentImporterModule implements ModuleLifecycle {
61   
62    private static final Logger log = LoggerFactory.getLogger(ContentImporterModule.class);
63   
64    private static final String CONTENT_BOOTSTRAP_DIR_PROPERTY_KEY = "magnolia.content.bootstrap.dir";
65   
66    private final String rootPath;
67    private final DirectoryWatcherService directoryWatcherService;
68    private final TaskCreatorWatcherCallback taskCreatorCallback;
69   
 
70  5 toggle @Inject
71    public ContentImporterModule(MagnoliaConfigurationProperties properties,
72    DirectoryWatcherService directoryWatcherService,
73    TaskCreatorWatcherCallback taskCreatorCallback) {
74  5 this.rootPath = properties.getProperty(CONTENT_BOOTSTRAP_DIR_PROPERTY_KEY);
75  5 this.directoryWatcherService = directoryWatcherService;
76  5 this.taskCreatorCallback = taskCreatorCallback;
77    }
78   
 
79  3 toggle @Override
80    public void start(ModuleLifecycleContext moduleLifecycleContext) {
81  3 if (StringUtils.isNotBlank(rootPath)) {
82  2 try {
83  2 Path path = Paths.get(rootPath);
84  2 log.info("Starting Content Importer module. Watching directory '{}' for files to import.", path);
85  2 directoryWatcherService.register(path, new AlwaysTruePredicate(), taskCreatorCallback);
86    } catch (IOException e) {
87  1 throw new RuntimeException("Could not start observation of content bootstrap files", e);
88    }
89    } else {
90  1 log.info("Content bootstrap directory is not defined with property of '{}' in magnolia.properties file, " +
91    "therefore observation is not started.", CONTENT_BOOTSTRAP_DIR_PROPERTY_KEY);
92    }
93    }
94   
 
95  1 toggle @Override
96    public void stop(ModuleLifecycleContext moduleLifecycleContext) {
97    }
98   
99    /**
100    * Predicate accepts all folders to be registered via {@link DirectoryWatcherService}.
101    */
 
102    private static class AlwaysTruePredicate implements Predicate<Path> {
 
103  0 toggle @Override
104    public boolean apply(Path ignored) {
105  0 return true;
106    }
107    }
108    }