Source Code Cross Referenced for SocketHandler.java in  » 6.0-JDK-Core » Collections-Jar-Zip-Logging-regex » java » util » logging » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » Collections Jar Zip Logging regex » java.util.logging 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 2000-2003 Sun Microsystems, Inc.  All Rights Reserved.
003         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004         *
005         * This code is free software; you can redistribute it and/or modify it
006         * under the terms of the GNU General Public License version 2 only, as
007         * published by the Free Software Foundation.  Sun designates this
008         * particular file as subject to the "Classpath" exception as provided
009         * by Sun in the LICENSE file that accompanied this code.
010         *
011         * This code is distributed in the hope that it will be useful, but WITHOUT
012         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014         * version 2 for more details (a copy is included in the LICENSE file that
015         * accompanied this code).
016         *
017         * You should have received a copy of the GNU General Public License version
018         * 2 along with this work; if not, write to the Free Software Foundation,
019         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020         *
021         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022         * CA 95054 USA or visit www.sun.com if you need additional information or
023         * have any questions.
024         */
025
026        package java.util.logging;
027
028        import java.io.*;
029        import java.net.*;
030
031        /**
032         * Simple network logging <tt>Handler</tt>.
033         * <p>
034         * <tt>LogRecords</tt> are published to a network stream connection.  By default
035         * the <tt>XMLFormatter</tt> class is used for formatting.
036         * <p>
037         * <b>Configuration:</b>
038         * By default each <tt>SocketHandler</tt> is initialized using the following
039         * <tt>LogManager</tt> configuration properties.  If properties are not defined
040         * (or have invalid values) then the specified default values are used.
041         * <ul>
042         * <li>   java.util.logging.SocketHandler.level
043         *	  specifies the default level for the <tt>Handler</tt>
044         *	  (defaults to <tt>Level.ALL</tt>).
045         * <li>   java.util.logging.SocketHandler.filter
046         * 	  specifies the name of a <tt>Filter</tt> class to use
047         *	  (defaults to no <tt>Filter</tt>).
048         * <li>   java.util.logging.SocketHandler.formatter
049         *	  specifies the name of a <tt>Formatter</tt> class to use 
050         *	  (defaults to <tt>java.util.logging.XMLFormatter</tt>).
051         * <li>   java.util.logging.SocketHandler.encoding 
052         *	  the name of the character set encoding to use (defaults to
053         *	  the default platform encoding).
054         * <li>   java.util.logging.SocketHandler.host
055         *	  specifies the target host name to connect to (no default).
056         * <li>   java.util.logging.SocketHandler.port
057         *	  specifies the target TCP port to use (no default).
058         * </ul>
059         * <p>
060         * The output IO stream is buffered, but is flushed after each
061         * <tt>LogRecord</tt> is written.
062         *
063         * @version 1.25, 05/05/07
064         * @since 1.4
065         */
066
067        public class SocketHandler extends StreamHandler {
068            private Socket sock;
069            private String host;
070            private int port;
071            private String portProperty;
072
073            // Private method to configure a SocketHandler from LogManager
074            // properties and/or default values as specified in the class
075            // javadoc.
076            private void configure() {
077                LogManager manager = LogManager.getLogManager();
078                String cname = getClass().getName();
079
080                setLevel(manager.getLevelProperty(cname + ".level", Level.ALL));
081                setFilter(manager.getFilterProperty(cname + ".filter", null));
082                setFormatter(manager.getFormatterProperty(cname + ".formatter",
083                        new XMLFormatter()));
084                try {
085                    setEncoding(manager.getStringProperty(cname + ".encoding",
086                            null));
087                } catch (Exception ex) {
088                    try {
089                        setEncoding(null);
090                    } catch (Exception ex2) {
091                        // doing a setEncoding with null should always work.
092                        // assert false;
093                    }
094                }
095                port = manager.getIntProperty(cname + ".port", 0);
096                host = manager.getStringProperty(cname + ".host", null);
097            }
098
099            /**
100             * Create a <tt>SocketHandler</tt>, using only <tt>LogManager</tt> properties
101             * (or their defaults).
102             * @throws IllegalArgumentException if the host or port are invalid or
103             *		are not specified as LogManager properties.
104             * @throws IOException if we are unable to connect to the target
105             *	       host and port.
106             */
107            public SocketHandler() throws IOException {
108                // We are going to use the logging defaults.
109                sealed = false;
110                configure();
111
112                try {
113                    connect();
114                } catch (IOException ix) {
115                    System.err.println("SocketHandler: connect failed to "
116                            + host + ":" + port);
117                    throw ix;
118                }
119                sealed = true;
120            }
121
122            /**
123             * Construct a <tt>SocketHandler</tt> using a specified host and port.
124             *
125             * The <tt>SocketHandler</tt> is configured based on <tt>LogManager</tt> 
126             * properties (or their default values) except that the given target host
127             * and port arguments are used. If the host argument is empty, but not
128             * null String then the localhost is used.
129             *
130             * @param host target host.
131             * @param port target port.
132             *
133             * @throws IllegalArgumentException if the host or port are invalid.
134             * @throws IOException if we are unable to connect to the target
135             *	       host and port.
136             */
137            public SocketHandler(String host, int port) throws IOException {
138                sealed = false;
139                configure();
140                sealed = true;
141                this .port = port;
142                this .host = host;
143                connect();
144            }
145
146            private void connect() throws IOException {
147                // Check the arguments are valid.
148                if (port == 0) {
149                    throw new IllegalArgumentException("Bad port: " + port);
150                }
151                if (host == null) {
152                    throw new IllegalArgumentException("Null host name: "
153                            + host);
154                }
155
156                // Try to open a new socket.
157                sock = new Socket(host, port);
158                OutputStream out = sock.getOutputStream();
159                BufferedOutputStream bout = new BufferedOutputStream(out);
160                setOutputStream(bout);
161            }
162
163            /**
164             * Close this output stream.
165             *
166             * @exception  SecurityException  if a security manager exists and if
167             *             the caller does not have <tt>LoggingPermission("control")</tt>.
168             */
169            public synchronized void close() throws SecurityException {
170                super .close();
171                if (sock != null) {
172                    try {
173                        sock.close();
174                    } catch (IOException ix) {
175                        // drop through.
176                    }
177                }
178                sock = null;
179            }
180
181            /**
182             * Format and publish a <tt>LogRecord</tt>.
183             *
184             * @param  record  description of the log event. A null record is
185             *                 silently ignored and is not published
186             */
187            public synchronized void publish(LogRecord record) {
188                if (!isLoggable(record)) {
189                    return;
190                }
191                super.publish(record);
192                flush();
193            }
194        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.