1 package org.apache.torque.generator.source;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.Iterator;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.torque.generator.configuration.ConfigurationException;
27 import org.apache.torque.generator.configuration.ConfigurationHandlers;
28 import org.apache.torque.generator.control.ControllerState;
29
30 /**
31 * The input for a generation process. It can contain several sources.
32 * In order to use the sources, the init() method must be called. After
33 * this method was called, no more setters may be called.
34 */
35 public abstract class SourceProvider implements Iterator<Source>
36 {
37 /** The class log. */
38 private static Log log = LogFactory.getLog(SourceProvider.class);
39
40 /**
41 * Whether the init method was already called.
42 */
43 private boolean initialized;
44
45 /**
46 * Initializes the source provider. Must be called before
47 * <code>hasNext()</code> or <code>next()</code> is invoked.
48 *
49 * @param configurationHandlers the configuration handlers, not null.
50 * @param controllerState the current controller state, not null.
51 *
52 * @throws ConfigurationException if initializing fails.
53 */
54 public final synchronized void init(
55 ConfigurationHandlers configurationHandlers,
56 ControllerState controllerState)
57 throws ConfigurationException
58 {
59 if (initialized)
60 {
61 log.warn("init() called more than once, ignoring this call");
62 return;
63 }
64 initInternal(configurationHandlers, controllerState);
65 initialized = true;
66 }
67
68 /**
69 * Resets the source provider. After this method is called,
70 * <code>init()</code> must be called again.
71 *
72 * @param configurationHandlers the configuration handlers, not null.
73 * @param controllerState the current controller state, not null.
74 *
75 * @throws ConfigurationException if resetting fails.
76 */
77 public final synchronized void reset(
78 ConfigurationHandlers configurationHandlers,
79 ControllerState controllerState)
80 throws ConfigurationException
81 {
82 if (!initialized)
83 {
84 log.warn("reset() called on uninitialized SourceProvider, "
85 + "ignoring this call");
86 return;
87 }
88 resetInternal(configurationHandlers, controllerState);
89 initialized = false;
90 }
91
92 /**
93 * Initializes the sources provided by this SourceProvider.
94 *
95 * @param configurationHandlers the configuration handlers, not null.
96 * @param controllerState the current controller state, not null.
97 *
98 * @throws ConfigurationException if initializing fails.
99 */
100 protected abstract void initInternal(
101 ConfigurationHandlers configurationHandlers,
102 ControllerState controllerState)
103 throws ConfigurationException;
104
105 /**
106 * Resets the sources provided by this SourceProvider.
107 *
108 * @param configurationHandlers the configuration handlers, not null.
109 * @param controllerState the current controller state, not null.
110 *
111 * @throws ConfigurationException if resetting fails.
112 */
113 protected abstract void resetInternal(
114 ConfigurationHandlers configurationHandlers,
115 ControllerState controllerState)
116 throws ConfigurationException;
117
118 /**
119 * Returns whether <code>init()</code> was already called.
120 *
121 * @return true if init() was already called, false otherwise.
122 */
123 public boolean isInit()
124 {
125 return initialized;
126 }
127
128 /**
129 * Returns a copy of this source provider in its initial state.
130 * This means the
131 * {@link #init(ConfigurationHandlers, ControllerState)}
132 * method of the new source provider must be called before it can be used.
133 *
134 * @throws ConfigurationException if the new SourceProvider cannot
135 * be initialized.
136 */
137 public abstract SourceProvider copy() throws ConfigurationException;
138
139 /**
140 * Copies settings which are not set in this source provider from another
141 * source provider. This only works if the type of the other source
142 * provider is known to this source provider.
143 * Only a subset of all properties are typically used for overwriting.
144 * No Properties which are already set are overwritten.
145 *
146 * @param sourceProvider the source provoder to copy the settings from.
147 */
148 public abstract void copyNotSetSettingsFrom(SourceProvider sourceProvider);
149 }