| 1 | package org.jtoolkit.essence.app.impl; |
| 2 | |
| 3 | import org.jetbrains.annotations.NotNull; |
| 4 | import org.jetbrains.annotations.Nullable; |
| 5 | import org.jtoolkit.essence.app.Container; |
| 6 | import org.jtoolkit.essence.app.Main; |
| 7 | import static org.jtoolkit.essence.app.pojo.impl.DataValueClass.buildArray; |
| 8 | import org.jtoolkit.essence.app.pojo.impl.PojoContext; |
| 9 | import org.jtoolkit.essence.concurrency.Immutable; |
| 10 | import org.jtoolkit.essence.utils.Named; |
| 11 | |
| 12 | import java.util.Collections; |
| 13 | import java.util.LinkedHashMap; |
| 14 | import java.util.List; |
| 15 | import java.util.Map; |
| 16 | |
| 17 | /* |
| 18 | Copyright 2006 Peter Lawrey |
| 19 | |
| 20 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 21 | you may not use this file except in compliance with the License. |
| 22 | You may obtain a copy of the License at |
| 23 | |
| 24 | http://www.apache.org/licenses/LICENSE-2.0 |
| 25 | |
| 26 | Unless required by applicable law or agreed to in writing, software |
| 27 | distributed under the License is distributed on an "AS IS" BASIS, |
| 28 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 29 | See the License for the specific language governing permissions and |
| 30 | limitations under the License. |
| 31 | */ |
| 32 | /** |
| 33 | * @author Peter Lawrey |
| 34 | */ |
| 35 | @Immutable |
| 36 | public class ComponentModel { |
| 37 | // private static final Log LOG = LogFactory.getLog(ComponentModel.class); |
| 38 | |
| 39 | private final ContainerProperty[] containerProperties; |
| 40 | private final ComponentBuilderData[] componentBuilders; |
| 41 | private final ComponentData[] components; |
| 42 | private final ComponentReferenceData[] componentReferences; |
| 43 | private final ComponentPropertyData[] componentProperties; |
| 44 | public final PojoContext pojoContext; |
| 45 | private final String[] contexts; |
| 46 | |
| 47 | public ComponentModel(@NotNull String name, @NotNull Map<String, List<String[]>> containerConfig) throws InstantiationException { |
| 48 | contexts = Main.generateContexts(name ); |
| 49 | pojoContext = new PojoContext(name, contexts); |
| 50 | pojoContext.putProperties(System.getenv()); |
| 51 | pojoContext.putProperties(System.getProperties()); |
| 52 | pojoContext.putProperty(Container.CONTAINER_NAME, name); |
| 53 | |
| 54 | String serverHostname = pojoContext.properties.getValue(Container.SERVER_HOSTNAME); |
| 55 | if (serverHostname == null) pojoContext.putProperty(Container.SERVER_HOSTNAME, Main.SERVER_NAME); |
| 56 | |
| 57 | containerProperties = buildArray(extract(containerConfig, "ContainerProperties"), ContainerProperty.class, pojoContext); |
| 58 | Map<String, String> properties = new LinkedHashMap<String, String>(); |
| 59 | for (ContainerProperty prop : containerProperties) |
| 60 | properties.put(prop.property, prop.value); |
| 61 | |
| 62 | String propertiesFiles = pojoContext.properties.getValue(Container.PROPERTIES_FILES); |
| 63 | if (propertiesFiles != null && propertiesFiles.length() > 0) |
| 64 | for(String file: propertiesFiles.split(", ?")) |
| 65 | pojoContext.loadPropertiesFile(file); |
| 66 | |
| 67 | pojoContext.putProperties(properties); |
| 68 | componentBuilders = buildArray(extract(containerConfig, "ComponentBuilders"), ComponentBuilderData.class, pojoContext); |
| 69 | components = buildArray(extract(containerConfig, "Components"), ComponentData.class, pojoContext); |
| 70 | componentReferences = buildArray(extract(containerConfig, "ComponentReferences"), ComponentReferenceData.class, pojoContext); |
| 71 | componentProperties = buildArray(extract(containerConfig, "ComponentProperties"), ComponentPropertyData.class, pojoContext); |
| 72 | } |
| 73 | |
| 74 | @NotNull public Map<String, String> getContainerProperties() { |
| 75 | Map<String, String> properties = new LinkedHashMap<String, String>(); |
| 76 | for (ContainerProperty prop : containerProperties) |
| 77 | properties.put(prop.property, prop.value); |
| 78 | return properties; |
| 79 | } |
| 80 | |
| 81 | @NotNull Map<String, ComponentBuilderData> getContainerBuilders() { |
| 82 | Map<String, ComponentBuilderData> builders = new LinkedHashMap<String, ComponentBuilderData>(); |
| 83 | for (ComponentBuilderData builder : componentBuilders) |
| 84 | builders.put(builder.name, builder); |
| 85 | return builders; |
| 86 | } |
| 87 | |
| 88 | @NotNull public String[] getComponentNames() { |
| 89 | String[] names = new String[components.length]; |
| 90 | for (int i = 0; i < components.length; i++) |
| 91 | names[i] = components[i].component; |
| 92 | return names; |
| 93 | } |
| 94 | |
| 95 | @Nullable ComponentData getComponentData(@NotNull String name) { |
| 96 | for (ComponentData cd : components) |
| 97 | if (cd.component.equals(name)) |
| 98 | return cd; |
| 99 | return null; |
| 100 | } |
| 101 | |
| 102 | @NotNull public Named.Source<String> getComponentReferences(@NotNull String name) { |
| 103 | Map<String, String> ret = new LinkedHashMap<String, String>(); |
| 104 | for (ComponentReferenceData cd : componentReferences) |
| 105 | if (cd.component.equals(name)) |
| 106 | ret.put(cd.reference, cd.componentReferenced); |
| 107 | return new Named.ContextSource<String>(name, new Named.MapSource<String>(ret), contexts); |
| 108 | } |
| 109 | |
| 110 | @NotNull public Named.Source<String> getComponentProperties(@NotNull String name) { |
| 111 | Map<String, String> ret = new LinkedHashMap<String, String>(); |
| 112 | for (ComponentPropertyData cd : componentProperties) |
| 113 | if (cd.component.equals(name)) |
| 114 | ret.put(cd.property, cd.value); |
| 115 | return new Named.ContextSource<String>(name, new Named.MapSource<String>(ret), contexts); |
| 116 | } |
| 117 | |
| 118 | private static List<String[]> extract(Map<String, List<String[]>> containerConfig, String name) { |
| 119 | List<String[]> strings = containerConfig.remove(name); |
| 120 | return strings == null ? Collections.<String[]>emptyList() : strings; |
| 121 | } |
| 122 | |
| 123 | static class ContainerProperty { |
| 124 | public final String property; |
| 125 | public final String value; |
| 126 | |
| 127 | @SuppressWarnings("UnusedDeclaration") |
| 128 | ContainerProperty(@NotNull String property, String value) { |
| 129 | this.property = property; |
| 130 | this.value = value; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | static class ComponentBuilderData { |
| 135 | public final String name; |
| 136 | public final Class componentBuilder; |
| 137 | public final String purpose; |
| 138 | |
| 139 | @SuppressWarnings("UnusedDeclaration") |
| 140 | ComponentBuilderData(String name, Class componentBuilder, String purpose) { |
| 141 | this.name = name; |
| 142 | this.componentBuilder = componentBuilder; |
| 143 | this.purpose = purpose; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static class ComponentData { |
| 148 | public final String component; |
| 149 | public final Class className; |
| 150 | public final String logicalProcess; |
| 151 | public final String purpose; |
| 152 | |
| 153 | @SuppressWarnings("UnusedDeclaration") |
| 154 | ComponentData(@NotNull String component, @NotNull Class className, @NotNull String logicalProcess, String purpose) { |
| 155 | this.component = component; |
| 156 | this.className = className; |
| 157 | this.logicalProcess = logicalProcess; |
| 158 | this.purpose = purpose; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | static class ComponentReferenceData { |
| 163 | public final String component; |
| 164 | public final String reference; |
| 165 | public final String componentReferenced; |
| 166 | |
| 167 | @SuppressWarnings("UnusedDeclaration") |
| 168 | ComponentReferenceData(@NotNull String component, @NotNull String reference, @Nullable String componentReferenced) { |
| 169 | this.component = component; |
| 170 | this.reference = reference; |
| 171 | this.componentReferenced = componentReferenced; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | static class ComponentPropertyData { |
| 176 | public final String component; |
| 177 | public final String property; |
| 178 | public final String value; |
| 179 | |
| 180 | @SuppressWarnings("UnusedDeclaration") |
| 181 | ComponentPropertyData(@NotNull String component, @NotNull String property, @NotNull String value) { |
| 182 | this.component = component; |
| 183 | this.property = property; |
| 184 | this.value = value; |
| 185 | } |
| 186 | } |
| 187 | } |