001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.plugin;
018
019import org.apache.activemq.broker.Broker;
020import org.apache.activemq.broker.BrokerPlugin;
021import org.slf4j.Logger;
022import org.slf4j.LoggerFactory;
023
024/**
025 * A Plugin which allows to force every incoming message to be PERSISTENT or
026 * NON-PERSISTENT.
027 *
028 * Useful, if you have set the broker usage policy to process ONLY persistent or
029 * ONLY non-persistent messages.
030 *
031 * @org.apache.xbean.XBean element="forcePersistencyModeBrokerPlugin"
032 */
033public class ForcePersistencyModeBrokerPlugin implements BrokerPlugin {
034    private static Logger LOG = LoggerFactory.getLogger(ForcePersistencyModeBrokerPlugin.class);
035    private boolean persistenceFlag = false;
036
037    /**
038     * Constructor
039     */
040    public ForcePersistencyModeBrokerPlugin() {}
041
042    /**
043     * @param broker
044     *
045     * @return the Broker
046     *
047     * @throws Exception
048     *
049     * @see org.apache.activemq.broker.BrokerPlugin#installPlugin(org.apache.activemq.broker.Broker)
050     */
051    @Override
052    public Broker installPlugin(Broker broker) throws Exception {
053        ForcePersistencyModeBroker pB = new ForcePersistencyModeBroker(broker);
054        pB.setPersistenceFlag(isPersistenceForced());
055        LOG.info("Installing ForcePersistencyModeBroker plugin: persistency enforced={}", pB.isPersistent());
056        return pB;
057    }
058
059    /**
060     * Sets the persistence mode.
061     *
062     * @param persistenceFlag
063     */
064    public void setPersistenceFlag(final boolean persistenceFlag) {
065        this.persistenceFlag = persistenceFlag;
066    }
067
068    /**
069     * @return the mode the (activated) plugin will set the message delivery
070     *         mode
071     */
072    public final boolean isPersistenceForced() {
073        return persistenceFlag;
074    }
075}