Organize logging into init, client service, and resource service threads
authorgilbert <gilbert@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Mon, 6 Jun 2005 13:11:28 +0000 (13:11 +0000)
committergilbert <gilbert@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Mon, 6 Jun 2005 13:11:28 +0000 (13:11 +0000)
git-svn-id: https://subversion.switch.ch/svn/shibboleth/java-idp/trunk@1604 ab3bd59b-922f-494d-bb5f-6f0a3c29deca

src/edu/internet2/middleware/shibboleth/serviceprovider/AssertionConsumerServlet.java
src/edu/internet2/middleware/shibboleth/serviceprovider/ContextListener.java [new file with mode: 0644]
src/edu/internet2/middleware/shibboleth/serviceprovider/FilterSupportImpl.java
src/edu/internet2/middleware/shibboleth/serviceprovider/ServiceProviderConfig.java
src/edu/internet2/middleware/shibboleth/serviceprovider/ServletContextInitializer.java
src/edu/internet2/middleware/shibboleth/serviceprovider/ShibBinding.java
src/edu/internet2/middleware/shibboleth/serviceprovider/XMLAAPImpl.java
src/edu/internet2/middleware/shibboleth/serviceprovider/XMLMetadataImpl.java

index 6214986..6fe474d 100644 (file)
@@ -48,7 +48,6 @@
  */
 package edu.internet2.middleware.shibboleth.serviceprovider;
 
-import java.io.File;
 import java.io.IOException;
 
 import javax.servlet.ServletContext;
@@ -59,21 +58,14 @@ import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.opensaml.SAMLException;
+import org.apache.log4j.Logger;
 import org.opensaml.SAMLBrowserProfile;
+import org.opensaml.SAMLException;
 import org.opensaml.SAMLResponse;
 import org.opensaml.SAMLBrowserProfile.BrowserProfileResponse;
 import org.w3c.dom.Element;
-import org.apache.log4j.FileAppender;
-import org.apache.log4j.Layout;
-import org.apache.log4j.Level;
-import org.apache.log4j.Logger;
-import org.apache.log4j.PatternLayout;
-import org.apache.xml.security.Init;
 
 import x0.maceShibbolethTargetConfig1.SessionsDocument.Sessions;
-
-import edu.internet2.middleware.commons.log4j.ThreadLocalAppender;
 import edu.internet2.middleware.shibboleth.common.Credentials;
 import edu.internet2.middleware.shibboleth.common.ShibBrowserProfile;
 import edu.internet2.middleware.shibboleth.metadata.MetadataException;
@@ -87,7 +79,7 @@ import edu.internet2.middleware.shibboleth.serviceprovider.ServiceProviderConfig
  */
 public class AssertionConsumerServlet extends HttpServlet {
 
-       private static Logger log = null;
+       private static Logger log = Logger.getLogger(AssertionConsumerServlet.class.getName());
        
        private static ServiceProviderContext context = ServiceProviderContext.getInstance();
        
@@ -102,43 +94,10 @@ public class AssertionConsumerServlet extends HttpServlet {
                super.init();
                ServletContext servletContext = this.getServletContext();
                
-               Init.init();
-
-               // Initialize logging specially
-               Logger targetLogger = Logger.getLogger("edu.internet2.middleware");
-               Logger samlLogger = Logger.getLogger("org.opensaml");
-               File diagdir = new File(servletContext.getRealPath("/diagnose"));
-               diagdir.mkdirs();
-               String logname = servletContext.getRealPath("/diagnose/initialize.log");
-               Layout initLayout = new PatternLayout("%d{HH:mm} %-5p %m%n");
-               
-               try {
-            FileAppender initLogAppender = new FileAppender(initLayout,logname);
-            ThreadLocalAppender threadAppender = new ThreadLocalAppender();
-            threadAppender.setLayout(initLayout);
-            targetLogger.setAdditivity(false);
-            targetLogger.addAppender(initLogAppender);
-            targetLogger.addAppender(threadAppender);
-            targetLogger.setLevel(Level.DEBUG);
-            samlLogger.addAppender(threadAppender);
-            samlLogger.setLevel(Level.DEBUG);
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
-               
-/*             ConsoleAppender rootAppender = new ConsoleAppender();
-               rootAppender.setWriter(new PrintWriter(System.out));
-               rootAppender.setName("stdout");
-               targetLogger.addAppender(rootAppender);
-
-               // rootAppender.setLayout(new PatternLayout("%-5p %-41X{serviceId} %d{ISO8601} (%c:%L) - %m%n"));
-               // Logger.getRootLogger().setLevel((Level) Level.DEBUG);
-               Logger.getRootLogger().setLevel((Level) Level.INFO);
-               rootAppender.setLayout(new PatternLayout("%d{ISO8601} %-5p %-41X{serviceId} - %m%n"));
-*/
-               log = Logger.getLogger(AssertionConsumerServlet.class.getName());
-               
+               // Note: the ServletContext should have been initialized by the Listener
                ServletContextInitializer.initServiceProvider(servletContext);
+               
+               // Establish linkage between the SP context and the RM Filter class
                AuthenticationFilter.setFilterSupport(new FilterSupportImpl());
        }
 
@@ -164,7 +123,7 @@ public class AssertionConsumerServlet extends HttpServlet {
             String ipaddr = request.getRemoteAddr();
             
             // URL of Resource that triggered authorization
-            // XXX: I added support to the profile for extracting TARGET, but
+            // I added support to the profile for extracting TARGET, but
             // it's not too critical in Java since you can grab it easily anyway.
             // Might be better in the 2.0 future though, since the bindings get trickier.
             String target = request.getParameter("TARGET");
@@ -295,8 +254,7 @@ public class AssertionConsumerServlet extends HttpServlet {
 
     protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
        throws ServletException, IOException {
-        // TODO Auto-generated method stub
-        super.doGet(arg0, arg1);
+       // Currently the Assertion Consumer does not receive a GET
     }
        
 
diff --git a/src/edu/internet2/middleware/shibboleth/serviceprovider/ContextListener.java b/src/edu/internet2/middleware/shibboleth/serviceprovider/ContextListener.java
new file mode 100644 (file)
index 0000000..6557b83
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package edu.internet2.middleware.shibboleth.serviceprovider;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+import javax.servlet.UnavailableException;
+
+import org.apache.log4j.FileAppender;
+import org.apache.log4j.Layout;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+import org.apache.xml.security.Init;
+
+import edu.internet2.middleware.commons.log4j.ThreadLocalAppender;
+
+/**
+ * A ContextListener gets control from the Servlet Container before
+ * any Servlets or Filters are loaded. It can perform the earliest 
+ * forms of initialization. Commonly, this is used to initialize log
+ * files or container systems (such as Spring). Initialization can
+ * done here or in the init routines of the Filters and individual
+ * Servlets. It is better to do it here logic that is common to all
+ * Servlets and Filters since you do not know what order they will
+ * be loaded. It is generally a good idea to have one on spec so
+ * that you can move logic around as needed.
+ */
+public class ContextListener implements ServletContextListener {
+       
+
+       // Initialization, parsing files, and setting up
+       public static final String SHIBBOLETH_INIT = "shibboleth.init";
+       private Logger initLogger = Logger.getLogger(SHIBBOLETH_INIT);
+       
+       // Authentication and Attribute processing, including SAML, Trust, 
+       // Metadata, etc. Because the SP doesn't control all the code, it is
+       // based on real classnames
+       private Logger clientLogger = Logger.getLogger("edu.internet2.middleware");
+       private Logger samlLogger = Logger.getLogger("org.opensaml");
+       
+       // Requests from the Resource Manager only touch the RequestMapper
+       // and Session Cache
+       public static final String SHIBBOLETH_SERVICE = "shibboleth.service";
+       private Logger serviceLogger = Logger.getLogger(SHIBBOLETH_SERVICE);
+
+
+       public void contextInitialized(ServletContextEvent servletContextEvent) {
+               ServletContext servletContext = servletContextEvent.getServletContext();
+               Init.init(); // Let XML Security go first
+               
+               Layout initLayout = new PatternLayout("%d{HH:mm} %-5p %m%n");
+               ThreadLocalAppender threadAppender = new ThreadLocalAppender();
+               threadAppender.setLayout(initLayout);
+               clientLogger.addAppender(threadAppender);
+               
+               // The init log location is represented as a URL in the web.xml
+               // We have to change this int a fully qualified path name
+               String initLogUrl = servletContext.getInitParameter("InitializationLog");
+               if (initLogUrl!=null)
+                       try {
+                               URI initLogURI = new URI(initLogUrl); 
+                               File initLogFile = new File(initLogURI);
+                               String logname = initLogFile.getAbsolutePath();
+                               FileAppender initLogAppender = new FileAppender(initLayout,logname);
+                               initLogger.addAppender(initLogAppender);
+                               initLogger.setLevel(Level.DEBUG);
+                       } catch (URISyntaxException e1) {
+                               servletContext.log("InitializationLog context parameter is not a valid URL", e1);
+                       } catch (IOException e1) {
+                               servletContext.log("InitializationLog context parameter does not point to a valid location",e1);
+                       }
+                       
+               
+               samlLogger.addAppender(threadAppender);
+               samlLogger.setLevel(Level.DEBUG);
+               
+               try {
+                       ServletContextInitializer.initServiceProvider(servletContext);
+               } catch (UnavailableException e) {
+                       // Do nothing now, Servlet will retry in a few milliseconds
+               }
+               
+       }
+
+       public void contextDestroyed(ServletContextEvent arg0) {
+               // Nothing interesting happens at the end
+       }
+
+}
index 89c0066..9e52c45 100644 (file)
@@ -26,6 +26,7 @@ import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
 
+import org.apache.log4j.Logger;
 import org.opensaml.SAMLException;
 
 import x0.maceShibbolethTargetConfig1.SessionsDocument.Sessions;
@@ -42,7 +43,8 @@ import edu.internet2.middleware.shibboleth.serviceprovider.ServiceProviderConfig
  */
 public class FilterSupportImpl implements FilterSupport {
     
-    public static ServiceProviderContext context = ServiceProviderContext.getInstance();
+    private static ServiceProviderContext context = ServiceProviderContext.getInstance();
+    private static Logger log = Logger.getLogger(ContextListener.SHIBBOLETH_SERVICE);
 
     /**
      * Given a Resource URL, go to the RequestMap logic to find an applicationId.
index 797707d..fa85875 100644 (file)
@@ -150,6 +150,7 @@ import java.util.Map;
 import java.util.TreeMap;
 
 import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
 import org.apache.xmlbeans.XmlException;
 import org.apache.xmlbeans.XmlOptions;
 import org.opensaml.SAMLAssertion;
@@ -178,6 +179,7 @@ import x0.maceShibbolethTargetConfig1.PathDocument.Path;
 import edu.internet2.middleware.shibboleth.aap.AAP;
 import edu.internet2.middleware.shibboleth.aap.AttributeRule;
 import edu.internet2.middleware.shibboleth.common.Credentials;
+import edu.internet2.middleware.shibboleth.common.ShibResource;
 import edu.internet2.middleware.shibboleth.common.ShibbolethConfigurationException;
 import edu.internet2.middleware.shibboleth.common.Trust;
 import edu.internet2.middleware.shibboleth.common.provider.ShibbolethTrust;
@@ -201,7 +203,7 @@ public class ServiceProviderConfig {
        // Map key prefix for inline plugin configuration elements 
        private static final String INLINEURN = "urn:inlineBS:ID";
     
-    private static Logger log = Logger.getLogger(ServiceProviderConfig.class);
+    private static Logger log = Logger.getLogger(ContextListener.SHIBBOLETH_INIT+".Config");
 
        private SPConfigType  // The XMLBean from the main config file
                config = null;    
@@ -429,6 +431,12 @@ public class ServiceProviderConfig {
                        throw new ShibbolethConfigurationException("Error while parsing shibboleth configuration");
                }
                
+               try {
+                       String loggerUrl = config.getLogger();
+                       PropertyConfigurator.configure(new URL(loggerUrl));
+               } catch (Exception e) {
+                       log.error("Cannot process logger attribute of SP configuration file.",e);
+               }
                
                Applications apps = config.getApplications(); // <Applications>
                
index 05f7857..9556624 100644 (file)
@@ -66,7 +66,7 @@ import edu.internet2.middleware.shibboleth.common.ShibbolethConfigurationExcepti
  */
 public class ServletContextInitializer {
        
-       private static Logger log = Logger.getLogger(ServletContextInitializer.class.getName());
+       private static Logger log = Logger.getLogger(ContextListener.SHIBBOLETH_INIT+".Initializer");
        private static ServiceProviderContext context   = ServiceProviderContext.getInstance();
 
        public static boolean initialized = false;
index 2dc50f1..b6c772a 100644 (file)
@@ -42,8 +42,6 @@ import org.opensaml.SAMLRequest;
 import org.opensaml.SAMLResponse;
 import org.opensaml.SAMLSOAPHTTPBinding;
 import org.opensaml.TrustException;
-import org.opensaml.provider.SOAPHTTPBindingProvider;
-
 import edu.internet2.middleware.shibboleth.common.Trust;
 import edu.internet2.middleware.shibboleth.metadata.AttributeAuthorityDescriptor;
 import edu.internet2.middleware.shibboleth.metadata.Endpoint;
index 3a74fe6..766a5bc 100644 (file)
@@ -30,7 +30,6 @@ package edu.internet2.middleware.shibboleth.serviceprovider;
 
 import java.util.Iterator;
 
-import org.apache.log4j.Logger;
 import org.opensaml.SAMLException;
 
 import edu.internet2.middleware.shibboleth.aap.provider.XMLAAPProvider;
@@ -58,7 +57,6 @@ public class XMLAAPImpl
        implements AAP,
        PluggableConfigurationComponent {
        
-       private static Logger log = Logger.getLogger(XMLAAPImpl.class);
     XMLAAPProvider realObject = null;
        
     public void initialize(Node dom) throws ShibbolethConfigurationException {
index 2f23919..80175be 100644 (file)
@@ -22,7 +22,6 @@
  */
 package edu.internet2.middleware.shibboleth.serviceprovider;
 
-import org.apache.log4j.Logger;
 import org.apache.xmlbeans.XmlException;
 import org.opensaml.SAMLException;
 import org.opensaml.artifact.Artifact;
@@ -48,7 +47,6 @@ class XMLMetadataImpl
                PluggableConfigurationComponent
        {
     
-    private static Logger log = Logger.getLogger(XMLMetadataImpl.class);
     
        XMLMetadataProvider realObject = null;