4e3c3ba1df9b08e8bb03c017bc793d3b65e48928
[controller.git] / opendaylight / commons / filter-valve / src / main / java / org / opendaylight / controller / filtervalve / cors / jaxb / Host.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.filtervalve.cors.jaxb;
10
11 import static com.google.common.base.Preconditions.checkArgument;
12 import static com.google.common.base.Preconditions.checkState;
13
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;
19 import java.util.Map;
20 import javax.xml.bind.annotation.XmlElement;
21 import javax.xml.bind.annotation.XmlRootElement;
22
23
24 /**
25  * Root element, arbitrarily named Host to match tomcat-server.xml, but does not allow specifying which host
26  * name to be matched.
27  */
28 @XmlRootElement(name = "Host")
29 public class 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;
34
35
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);
42         }
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);
49         }
50         contextMap = Collections.unmodifiableMap(new HashMap<>(contextMap));
51         contexts = Collections.unmodifiableList(new ArrayList<>(contexts));
52         initialized = true;
53     }
54
55     public Optional<Context> findContext(String contextPath) {
56         checkState(initialized, "Not initialized");
57         Context context = contextMap.get(contextPath);
58         return Optional.fromNullable(context);
59     }
60
61     @XmlElement(name = "Context")
62     public List<Context> getContexts() {
63         return contexts;
64     }
65
66     public void setContexts(List<Context> contexts) {
67         checkArgument(initialized == false, "Already initialized");
68         this.contexts = contexts;
69     }
70
71     @XmlElement(name = "filter-template")
72     public List<Filter> getFilterTemplates() {
73         return filterTemplates;
74     }
75
76     public void setFilterTemplates(List<Filter> filterTemplates) {
77         checkArgument(initialized == false, "Already initialized");
78         this.filterTemplates = filterTemplates;
79     }
80 }