1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20
21
22 package org.apache.james.domain;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Locale;
27
28 import org.apache.avalon.framework.configuration.Configurable;
29 import org.apache.avalon.framework.configuration.Configuration;
30 import org.apache.avalon.framework.configuration.ConfigurationException;
31
32 /**
33 * Mimic the old behavoir of JAMES
34 */
35 public class XMLDomainList extends AbstractDomainList implements Configurable {
36
37 private List domainNames = null;
38
39 private boolean managementDisabled = false;
40
41 /**
42 * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
43 */
44 public void configure(Configuration arg0) throws ConfigurationException {
45 Configuration conf = arg0.getChild("domainnames");
46 if (conf != null) {
47
48 Configuration[] serverNameConfs = conf.getChildren( "domainname" );
49 for ( int i = 0; i < serverNameConfs.length; i++ ) {
50 addDomainInternal( serverNameConfs[i].getValue());
51 }
52
53 Configuration autoConf = arg0.getChild("autodetect");
54 if (autoConf != null) {
55 setAutoDetect(autoConf.getValueAsBoolean(true));
56 }
57
58 Configuration autoIPConf = arg0.getChild("autodetectIP");
59 if (autoConf != null) {
60 setAutoDetectIP(autoIPConf.getValueAsBoolean(true));
61 }
62 }
63 }
64
65
66 /**
67 * @see org.apache.james.domain.AbstractDomainList#getDomainListInternal()
68 */
69 protected List getDomainListInternal() {
70 // TODO: Remove temporary fix!
71 // This is set to true to get sure now new domain can get added or removed
72 // after the domains were retrieved by James.java. See is a workaround!
73 managementDisabled = true;
74 return new ArrayList(domainNames);
75 }
76
77 /**
78 * @see org.apache.james.api.domainlist.DomainList#containsDomain(java.lang.String)
79 */
80 public boolean containsDomain(String domains) {
81 if (domainNames == null) return false;
82 return domainNames.contains(domains);
83 }
84
85 /**
86 * The added domains will only added in memory!
87 *
88 * @see org.apache.james.domain.AbstractDomainList#addDomainInternal(java.lang.String)
89 */
90 protected boolean addDomainInternal(String domain) {
91 // TODO: Remove later. Temporary fix to get sure no domains can be added to the XMLDomainList
92 if (managementDisabled) throw new UnsupportedOperationException("Management not supported");
93
94 if (domainNames == null) {
95 domainNames = new ArrayList();
96 }
97
98 String newDomain = domain.toLowerCase(Locale.US);
99 if (containsDomain(newDomain) == false) {
100 domainNames.add(newDomain);
101 return true;
102 } else {
103 return false;
104 }
105 }
106
107 /**
108 * @see org.apache.james.domain.AbstractDomainList#removeDomainInternal(java.lang.String)
109 */
110 protected boolean removeDomainInternal(String domain) {
111 // TODO: Remove later. Temporary fix to get sure no domains can be added to the XMLDomainList
112 if (managementDisabled) throw new UnsupportedOperationException("Management not supported");
113
114 if (domainNames == null) return false;
115 return domainNames.remove(domain.toLowerCase(Locale.US));
116 }
117 }