File: StoreFactoryRule.java

package info (click to toggle)
tomcat10 10.1.52-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,900 kB
  • sloc: java: 375,756; xml: 59,410; jsp: 4,741; sh: 1,381; perl: 324; makefile: 25; ansic: 14
file content (109 lines) | stat: -rw-r--r-- 4,139 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.catalina.storeconfig;

import org.apache.tomcat.util.digester.Rule;
import org.xml.sax.Attributes;

/**
 * <p>
 * Rule that creates a new <code>IStoreFactory</code> instance, and associates it with the top object on the stack
 * (which must implement <code>IStoreFactory</code>).
 * </p>
 */

public class StoreFactoryRule extends Rule {

    // ----------------------------------------------------------- Constructors

    /**
     * Construct a new instance of this Rule.
     *
     * @param storeFactoryClass     Default name of the StoreFactory implementation class to be created
     * @param attributeName         Name of the attribute that optionally includes an override name of the IStoreFactory
     *                                  class
     * @param storeAppenderClass    The store appender class
     * @param appenderAttributeName The attribute name for the store appender class
     */
    public StoreFactoryRule(String storeFactoryClass, String attributeName, String storeAppenderClass,
            String appenderAttributeName) {

        this.storeFactoryClass = storeFactoryClass;
        this.attributeName = attributeName;
        this.appenderAttributeName = appenderAttributeName;
        this.storeAppenderClass = storeAppenderClass;

    }

    // ----------------------------------------------------- Instance Variables

    /**
     * The attribute name of an attribute that can override the implementation class name.
     */
    private final String attributeName;

    private final String appenderAttributeName;

    /**
     * The name of the <code>IStoreFactory</code> implementation class.
     */
    private final String storeFactoryClass;

    private final String storeAppenderClass;

    // --------------------------------------------------------- Public Methods

    @Override
    public void begin(String namespace, String name, Attributes attributes) throws Exception {

        IStoreFactory factory = (IStoreFactory) newInstance(attributeName, storeFactoryClass, attributes);
        StoreAppender storeAppender =
                (StoreAppender) newInstance(appenderAttributeName, storeAppenderClass, attributes);
        factory.setStoreAppender(storeAppender);

        // Add this StoreFactory to our associated component
        StoreDescription desc = (StoreDescription) digester.peek(0);
        StoreRegistry registry = (StoreRegistry) digester.peek(1);
        factory.setRegistry(registry);
        desc.setStoreFactory(factory);

    }

    /**
     * Create new instance from attribute className!
     *
     * @param attr        class Name attribute
     * @param defaultName Default Class
     * @param attributes  current digester attribute elements
     *
     * @return new configured object instance
     *
     * @throws ReflectiveOperationException Error creating an instance
     */
    protected Object newInstance(String attr, String defaultName, Attributes attributes)
            throws ReflectiveOperationException {
        String className = defaultName;
        if (attr != null) {
            String value = attributes.getValue(attr);
            if (value != null) {
                className = value;
            }
        }
        Class<?> clazz = Class.forName(className);
        return clazz.getConstructor().newInstance();
    }
}