001/*
002 * (C) Copyright 2015 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 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.blob;
020
021import java.util.LinkedHashMap;
022import java.util.Map;
023
024import org.nuxeo.common.xmap.annotation.XNode;
025import org.nuxeo.common.xmap.annotation.XNodeMap;
026import org.nuxeo.common.xmap.annotation.XObject;
027import org.nuxeo.ecm.core.api.NuxeoException;
028
029/**
030 * Descriptor for a {@link BlobDispatcher} and its configuration.
031 *
032 * @since 7.3
033 */
034@XObject(value = "blobdispatcher")
035public class BlobDispatcherDescriptor {
036
037    public BlobDispatcherDescriptor() {
038    }
039
040    @XNode("class")
041    public Class<? extends BlobDispatcher> klass;
042
043    @XNodeMap(value = "property", key = "@name", type = LinkedHashMap.class, componentType = String.class)
044    public Map<String, String> properties = new LinkedHashMap<>();
045
046    private BlobDispatcher instance;
047
048    public synchronized BlobDispatcher getBlobDispatcher() {
049        if (instance == null) {
050            if (klass == null) {
051                throw new NuxeoException("Missing class in blob dispatcher descriptor");
052            }
053            BlobDispatcher blobDispatcher;
054            try {
055                blobDispatcher = klass.newInstance();
056            } catch (ReflectiveOperationException e) {
057                throw new NuxeoException(e);
058            }
059            blobDispatcher.initialize(properties);
060            instance = blobDispatcher;
061        }
062        return instance;
063    }
064
065}