View Javadoc
1   /**
2    * Copyright 2014 Internet2
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  /*
17   * @author mchyzer
18   * $Id: SubjectCheckConfig.java,v 1.3 2009-03-22 02:49:27 mchyzer Exp $
19   */
20  package edu.internet2.middleware.subject;
21  
22  import java.net.URL;
23  import java.util.Collection;
24  
25  import javax.naming.NameNotFoundException;
26  
27  import org.apache.commons.lang.exception.ExceptionUtils;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  import edu.internet2.middleware.grouper.app.gsh.GrouperShell;
32  import edu.internet2.middleware.grouper.cfg.GrouperConfig;
33  import edu.internet2.middleware.subject.provider.SourceManager;
34  
35  /**
36   * make sure the subject.xml config is correct
37   */
38  public class SubjectCheckConfig {
39  
40    /** */
41    public static final String GROUPER_TEST_SUBJECT_BY_ID = "grouperTestSubjectByIdOnStartupASDFGHJ";
42  
43    /** */
44    public static final String SUBJECT_ID_TO_FIND_ON_CHECK_CONFIG = "subjectIdToFindOnCheckConfig";
45  
46    /** */
47    public static final String FIND_SUBJECT_BY_ID_ON_CHECK_CONFIG = "findSubjectByIdOnCheckConfig";
48    
49    /** logger */
50    private static Log log = edu.internet2.middleware.grouper.util.GrouperUtil.getLog(SubjectCheckConfig.class);
51  
52    /**
53     * make surce classpath file is there
54     * @param resourceName
55     * @return true if ok
56     */
57    public static boolean checkConfig(String resourceName) {
58      boolean foundResource = false;
59      //first, there must be a subject.properties
60      try {
61        //get the url of the navigation file
62        ClassLoader cl = SubjectCheckConfig.class.getClassLoader();
63  
64        URL url = cl.getResource(resourceName);
65  
66        foundResource = url != null;
67      } catch (Exception e) {
68        //this means it cant be found
69        log.info("Exception looking for " + resourceName, e);
70      }
71      if (!foundResource) {
72        String error = "Cant find required resource on classpath: " + resourceName;
73        //this is serious, lets go out and error
74        System.err.println("Subject API error: " + error);
75        log.error(error);
76      }
77      return foundResource;
78    }
79    
80    /**
81     * check the subject config
82     */
83    public static void checkConfig() {
84      
85      //at this point, we have a subject.properties...  now check it out
86      Collection<Source> sources = null;
87      
88      boolean exceptionIfProblem = GrouperShell.runFromGsh && GrouperConfig.retrieveConfig().propertyValueBoolean("gsh.exitOnSubjectCheckConfigProblem", true);
89      
90      try {
91        sources = SourceManager.getInstance().getSources();
92      } catch (Exception e) {
93        String error = "problem initting sources from subject.properties";
94        System.err.println("Subject API error: " + error + ", " + ExceptionUtils.getFullStackTrace(e));
95        log.error(error, e);
96        if (exceptionIfProblem) {
97          throw new RuntimeException(error, e);
98        }
99        return;
100     }
101     int sourceCount = 0;
102     for (Source source: sources) {
103       
104       sourceCount++;
105       String error = "error with subject source id: " + source.getId() + ", name: " + source.getName()
106         + ", ";
107       try {
108         source.checkConfig();
109         
110         String findSubjectOnCheckConfigString = source.getInitParam(FIND_SUBJECT_BY_ID_ON_CHECK_CONFIG);
111         boolean findSubjectOnCheckConfig = SubjectUtils.booleanValue(findSubjectOnCheckConfigString, true);
112         
113         if (findSubjectOnCheckConfig) {
114           String subjectToFindOnCheckConfig = source.getInitParam(SUBJECT_ID_TO_FIND_ON_CHECK_CONFIG);
115           subjectToFindOnCheckConfig = SubjectUtils.defaultIfBlank(subjectToFindOnCheckConfig, GROUPER_TEST_SUBJECT_BY_ID);
116           source.getSubject(subjectToFindOnCheckConfig, false);
117         }
118 
119       } catch (Exception e) {
120         String theError = error + "problem with getSubject by id, in subject.properties: search searchSubject: ";
121         if (ExceptionUtils.getRootCause(e) != null && ExceptionUtils.getRootCause(e) instanceof NameNotFoundException) {
122           theError += "The underlying exception is NameNotFoundException.  Be sure that the base dn values in your subject.properties are correct and relative to the base dn specified in the LDAP url in your grouper-loader.properties configuration.  Note that if your LDAP url in grouper-loader.properties has a base dn, then that base dn should not be included in subject.properties.  See subject.base.properties for documentation and examples.";
123         }
124         System.err.println("Subject API error: " + theError + ", " + ExceptionUtils.getFullStackTrace(e));
125         log.error(theError, e);
126         
127         if (exceptionIfProblem) {
128           throw new RuntimeException(theError, e);
129         }
130         continue;
131       }
132       
133       try {
134         
135         String findSubjectOnCheckConfigString = source.getInitParam("findSubjectByIdentifiedOnCheckConfig");
136         boolean findSubjectOnCheckConfig = SubjectUtils.booleanValue(findSubjectOnCheckConfigString, true);
137         
138         if (findSubjectOnCheckConfig) {
139           String subjectIdentifierToFindOnCheckConfig = source.getInitParam("subjectIdentifierToFindOnCheckConfig");
140           subjectIdentifierToFindOnCheckConfig = SubjectUtils.defaultIfBlank(subjectIdentifierToFindOnCheckConfig, "grouperTestSubjectByIdentifierOnStartupASDFGHJ");
141           source.getSubjectByIdentifier(subjectIdentifierToFindOnCheckConfig, false);
142         }
143 
144       } catch (SubjectNotFoundException snfe) {
145         //good!
146       } catch (Exception e) {
147         String theError = error + "problem with getSubject by identifier, in subject.properties: searchType searchSubjectByIdentifier: ";
148         if (ExceptionUtils.getRootCause(e) != null && ExceptionUtils.getRootCause(e) instanceof NameNotFoundException) {
149           theError += "The underlying exception is NameNotFoundException.  Be sure that the base dn values in your subject.properties are correct and relative to the base dn specified in the LDAP url in your grouper-loader.properties configuration.  Note that if your LDAP url in grouper-loader.properties has a base dn, then that base dn should not be included in subject.properties.  See subject.base.properties for documentation and examples.";
150         }
151         System.err.println("Subject API error: " + theError + ", " + ExceptionUtils.getFullStackTrace(e));
152         log.error(theError, e);
153         
154         if (exceptionIfProblem) {
155           throw new RuntimeException(theError, e);
156         }
157         continue;
158       }
159     
160       try {
161         
162         String findSubjectOnCheckConfigString = source.getInitParam("findSubjectByStringOnCheckConfig");
163         boolean findSubjectOnCheckConfig = SubjectUtils.booleanValue(findSubjectOnCheckConfigString, true);
164         
165         if (findSubjectOnCheckConfig) {
166           String stringToFindOnCheckConfig = source.getInitParam("stringToFindOnCheckConfig");
167           stringToFindOnCheckConfig = SubjectUtils.defaultIfBlank(stringToFindOnCheckConfig, "grouperTestStringOnStartupASDFGHJ");
168           source.search(stringToFindOnCheckConfig);
169         }
170         
171       } catch (Exception e) {
172         String theError = error + "problem with search, in subject.properties: serachType search: ";
173         if (ExceptionUtils.getRootCause(e) != null && ExceptionUtils.getRootCause(e) instanceof NameNotFoundException) {
174           theError += "The underlying exception is NameNotFoundException.  Be sure that the base dn values in your subject.properties are correct and relative to the base dn specified in the LDAP url in your grouper-loader.properties configuration.  Note that if your LDAP url in grouper-loader.properties has a base dn, then that base dn should not be included in subject.properties.  See subject.base.properties for documentation and examples.";
175         }
176         System.err.println("Subject API error: " + theError + ", " + ExceptionUtils.getFullStackTrace(e));
177         log.error(theError, e);
178         
179         if (exceptionIfProblem) {
180           throw new RuntimeException(theError, e);
181         }
182         continue;
183       }
184     }
185     if (sourceCount == 0) {
186       System.err.println("Subject API warning: there are no sources available from subject.properties");
187       log.warn("there are no sources available from subject.properties");
188     }
189 
190   }
191   
192 }