2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.filtervalve.cors.jaxb;
11 import static com.google.common.base.Preconditions.checkArgument;
12 import static com.google.common.base.Preconditions.checkState;
14 import com.google.common.base.Optional;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.List;
20 import javax.xml.bind.annotation.XmlElement;
21 import javax.xml.bind.annotation.XmlRootElement;
25 * Root element, arbitrarily named Host to match tomcat-server.xml, but does not allow specifying which host
28 @XmlRootElement(name = "Host")
30 private List<Context> contexts = new ArrayList<>();
31 private List<Filter> filterTemplates = new ArrayList<>();
32 private boolean initialized;
33 private Map<String, Context> contextMap;
36 public synchronized void initialize(String fileName) {
37 checkState(initialized == false, "Already initialized");
38 Map<String, Filter> namesToTemplates = new HashMap<>();
39 for (Filter template : filterTemplates) {
40 template.initializeTemplate();
41 namesToTemplates.put(template.getFilterName(), template);
43 contextMap = new HashMap<>();
44 for (Context context : getContexts()) {
45 checkState(contextMap.containsKey(context.getPath()) == false,
46 "Context {} already defined in {}", context.getPath(), fileName);
47 context.initialize(fileName, namesToTemplates);
48 contextMap.put(context.getPath(), context);
50 contextMap = Collections.unmodifiableMap(new HashMap<>(contextMap));
51 contexts = Collections.unmodifiableList(new ArrayList<>(contexts));
55 public Optional<Context> findContext(String contextPath) {
56 checkState(initialized, "Not initialized");
57 Context context = contextMap.get(contextPath);
58 return Optional.fromNullable(context);
61 @XmlElement(name = "Context")
62 public List<Context> getContexts() {
66 public void setContexts(List<Context> contexts) {
67 checkArgument(initialized == false, "Already initialized");
68 this.contexts = contexts;
71 @XmlElement(name = "filter-template")
72 public List<Filter> getFilterTemplates() {
73 return filterTemplates;
76 public void setFilterTemplates(List<Filter> filterTemplates) {
77 checkArgument(initialized == false, "Already initialized");
78 this.filterTemplates = filterTemplates;