001/*
002 * (C) Copyright 2014 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 *     Anahide Tchertchian
018 */
019package org.nuxeo.targetplatforms.core.service;
020
021import org.nuxeo.ecm.directory.DirectoryException;
022import org.nuxeo.ecm.directory.Session;
023import org.nuxeo.ecm.directory.api.DirectoryService;
024import org.nuxeo.runtime.api.Framework;
025
026/**
027 * Updater in charge of override directory changes.
028 *
029 * @since 5.7.1
030 */
031public abstract class DirectoryUpdater {
032
033    public static final String DEFAULT_DIR = "targetPlatformOverrides";
034
035    public static final String SCHEMA = "target_platform_override";
036
037    public static final String DEPRECATED_PROP = "deprecated";
038
039    public static final String ENABLED_PROP = "enabled";
040
041    public static final String RESTRICTED_PROP = "restricted";
042
043    public static final String TRIAL_PROP = "trial";
044
045    public static final String DEFAULT_PROP = "default";
046
047    protected String dirName;
048
049    public DirectoryUpdater(String dirName) {
050        super();
051        this.dirName = dirName;
052    }
053
054    public abstract void run(DirectoryService service, Session session);
055
056    public void run() {
057        Session session = null;
058        try {
059            // check if entry already exists
060            DirectoryService service = Framework.getService(DirectoryService.class);
061            session = service.open(dirName);
062            run(service, session);
063        } finally {
064            if (session != null) {
065                session.close();
066            }
067        }
068    }
069
070}