001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.camel;
019
020import static org.slf4j.LoggerFactory.getLogger;
021
022import java.util.Map;
023
024import org.apache.camel.CamelContext;
025import org.apache.camel.Endpoint;
026import org.apache.camel.impl.UriEndpointComponent;
027import org.slf4j.Logger;
028import org.springframework.transaction.PlatformTransactionManager;
029
030/**
031 * Represents the component that manages {@link FcrepoEndpoint}.
032 * @author Aaron Coburn
033 * @since October 20, 2014
034 */
035public class FcrepoComponent extends UriEndpointComponent {
036
037    private FcrepoConfiguration configuration;
038
039    private PlatformTransactionManager transactionManager;
040
041    private static final Logger LOGGER  = getLogger(FcrepoComponent.class);
042
043    /**
044     * Create a FcrepoComponent independent of a CamelContext.
045     */
046    public FcrepoComponent() {
047        super(FcrepoEndpoint.class);
048    }
049
050    /**
051     * Given a CamelContext, create a FcrepoComponent instance.
052     * @param context the camel context for the component.
053     */
054    public FcrepoComponent(final CamelContext context) {
055        super(context, FcrepoEndpoint.class);
056    }
057
058    /**
059     * Given a FcrepoConfiguration, create a FcrepoComponent instance.
060     * @param config the component-wide configuration.
061     */
062    public FcrepoComponent(final FcrepoConfiguration config) {
063        super(FcrepoEndpoint.class);
064        this.configuration = config;
065    }
066
067    /**
068     * Get the component's configuration.
069     * @return the configuration for the component.
070     */
071    public FcrepoConfiguration getConfiguration() {
072        if (configuration == null) {
073            configuration = new FcrepoConfiguration();
074        }
075        return configuration;
076    }
077
078    /**
079     * Set the component's configuration.
080     * @param config the configuration settings for the component.
081     */
082    public void setConfiguration(final FcrepoConfiguration config) {
083        this.configuration = config;
084    }
085
086    /**
087     * Set the transaction manager for the component
088     *
089     * @param transactionManager the transaction manager for this component
090     */
091    public void setTransactionManager(final PlatformTransactionManager transactionManager) {
092        this.transactionManager = transactionManager;
093    }
094
095    /**
096     * Get the transaction manager for the component
097     *
098     * @return the transaction manager for this component
099     */
100    public PlatformTransactionManager getTransactionManager() {
101        return transactionManager;
102    }
103
104    /**
105     * set the authUsername value component-wide.
106     * @param username the authentication username.
107     */
108    public void setAuthUsername(final String username) {
109        getConfiguration().setAuthUsername(username);
110    }
111
112    /**
113     * set the authPassword value component-wide.
114     * @param password the authentication password.
115     */
116    public void setAuthPassword(final String password) {
117        getConfiguration().setAuthPassword(password);
118    }
119
120    /**
121     * set the authHost value component-wide.
122     * @param host the authentication host realm.
123     */
124    public void setAuthHost(final String host) {
125        getConfiguration().setAuthHost(host);
126    }
127
128    /**
129     * set the baseUrl component-wide
130     * @param baseUrl the repository root
131     */
132    public void setBaseUrl(final String baseUrl) {
133        getConfiguration().setBaseUrl(baseUrl);
134    }
135
136    /**
137     *  Create an Endpoint from a fcrepo uri along with an optional path value and attributes.
138     *  @param uri the fcrepo uri identifying the repository hostname and port
139     *  @param remaining the string identifying the repository path
140     *  @param parameters any optional attributes added to the endpoint
141     *  @return the camel endpoint
142     */
143    @Override
144    protected Endpoint createEndpoint(final String uri, final String remaining, final Map<String, Object> parameters) {
145        final FcrepoConfiguration newConfig;
146        if (configuration == null) {
147            newConfig = new FcrepoConfiguration();
148        } else {
149            newConfig = configuration.clone();
150        }
151
152        final Endpoint endpoint = new FcrepoEndpoint(uri, remaining, this, newConfig);
153        endpoint.configureProperties(parameters);
154        LOGGER.debug("Created Fcrepo Endpoint [{}]", endpoint);
155        return endpoint;
156    }
157}