Added: logging/site/trunk/docs/log4j/companions/receivers/xref/org/apache/log4j/net/SocketHubReceiver.html URL: http://svn.apache.org/viewvc/logging/site/trunk/docs/log4j/companions/receivers/xref/org/apache/log4j/net/SocketHubReceiver.html?view=auto&rev=558362 ============================================================================== --- logging/site/trunk/docs/log4j/companions/receivers/xref/org/apache/log4j/net/SocketHubReceiver.html (added) +++ logging/site/trunk/docs/log4j/companions/receivers/xref/org/apache/log4j/net/SocketHubReceiver.html Sat Jul 21 10:50:33 2007 @@ -0,0 +1,404 @@ + + + +SocketHubReceiver xref + + + +
View Javadoc
+
+1   /*
+2    * Licensed to the Apache Software Foundation (ASF) under one or more
+3    * contributor license agreements.  See the NOTICE file distributed with
+4    * this work for additional information regarding copyright ownership.
+5    * The ASF licenses this file to You under the Apache License, Version 2.0
+6    * (the "License"); you may not use this file except in compliance with
+7    * the License.  You may obtain a copy of the License at
+8    *
+9    *      http://www.apache.org/licenses/LICENSE-2.0
+10   *
+11   * Unless required by applicable law or agreed to in writing, software
+12   * distributed under the License is distributed on an "AS IS" BASIS,
+13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+14   * See the License for the specific language governing permissions and
+15   * limitations under the License.
+16   */
+17  
+18  package org.apache.log4j.net;
+19  
+20  import org.apache.log4j.plugins.Plugin;
+21  import org.apache.log4j.plugins.Receiver;
+22  import org.apache.log4j.spi.LoggerRepository;
+23  
+24  import java.io.IOException;
+25  import java.net.Socket;
+26  import java.util.ArrayList;
+27  import java.util.Collections;
+28  import java.util.Iterator;
+29  import java.util.List;
+30  
+31  /***
+32    SocketHubReceiver receives a remote logging event on a configured
+33    socket and "posts" it to a LoggerRepository as if the event was
+34    generated locally. This class is designed to receive events from
+35    the SocketHubAppender class (or classes that send compatible events).
+36  
+37    <p>Once the event has been "posted", it will be handled by the
+38    appenders currently configured in the LoggerRespository.
+39  
+40    @author Mark Womack
+41    @author Ceki G&uuml;lc&uuml;
+42    @author Paul Smith (psmith@apache.org)
+43    @since 1.3
+44  */
+45  public class SocketHubReceiver
+46  extends Receiver implements SocketNodeEventListener, PortBased {
+47  
+48      /***
+49       * Default reconnection delay.
+50       */
+51    static final int DEFAULT_RECONNECTION_DELAY   = 30000;
+52  
+53      /***
+54       * Host.
+55       */
+56    protected String host;
+57  
+58      /***
+59       * Port.
+60       */
+61    protected int port;
+62      /***
+63       * Reconnection delay.
+64       */
+65    protected int reconnectionDelay = DEFAULT_RECONNECTION_DELAY;
+66  
+67      /***
+68       * Active.
+69       */
+70    protected boolean active = false;
+71  
+72      /***
+73       * Connector.
+74       */
+75    protected Connector connector;
+76  
+77      /***
+78       * Socket.
+79       */
+80    protected Socket socket;
+81  
+82      /***
+83       * Listener list.
+84       */
+85    private List listenerList = Collections.synchronizedList(new ArrayList());
+86  
+87      /***
+88       * Create new instance.
+89       */
+90    public SocketHubReceiver() {
+91       super();
+92    }
+93  
+94      /***
+95       * Create new instance.
+96       * @param h host
+97       * @param p port
+98       */
+99    public SocketHubReceiver(final String h,
+100                            final int p) {
+101     super();
+102     host = h;
+103     port = p;
+104   }
+105 
+106     /***
+107      * Create new instance.
+108      * @param h host
+109      * @param p port
+110      * @param repo logger repository
+111      */
+112   public SocketHubReceiver(final String h,
+113                            final int p,
+114                            final LoggerRepository repo) {
+115     super();
+116     host = h;
+117     port = p;
+118     repository = repo;
+119   }
+120 
+121   /***
+122    * Adds a SocketNodeEventListener to this receiver to be notified
+123    * of SocketNode events.
+124    * @param l listener
+125    */
+126   public void addSocketNodeEventListener(final SocketNodeEventListener l) {
+127     listenerList.add(l);
+128   }
+129 
+130   /***
+131    * Removes a specific SocketNodeEventListener from this instance
+132    * so that it will no  longer be notified of SocketNode events.
+133    * @param l listener
+134    */
+135   public void removeSocketNodeEventListener(
+136           final SocketNodeEventListener l) {
+137     listenerList.remove(l);
+138   }
+139 
+140   /***
+141     Get the remote host to connect to for logging events.
+142     @return host
+143    */
+144   public String getHost() {
+145     return host;
+146   }
+147 
+148   /***
+149    * Configures the Host property, this will require activateOptions
+150    * to be called for this to take effect.
+151    * @param remoteHost address of remote host.
+152    */
+153   public void setHost(final String remoteHost) {
+154     this.host = remoteHost;
+155   }
+156   /***
+157     Set the remote host to connect to for logging events.
+158    Equivalent to setHost.
+159    @param remoteHost address of remote host.
+160    */
+161   public void setPort(final String remoteHost) {
+162     host = remoteHost;
+163   }
+164 
+165   /***
+166     Get the remote port to connect to for logging events.
+167    @return port
+168    */
+169   public int getPort() {
+170     return port;
+171   }
+172 
+173   /***
+174     Set the remote port to connect to for logging events.
+175     @param p port
+176    */
+177   public void setPort(final int p) {
+178     this.port = p;
+179   }
+180 
+181   /***
+182      The <b>ReconnectionDelay</b> option takes a positive integer
+183      representing the number of milliseconds to wait between each
+184      failed connection attempt to the server. The default value of
+185      this option is 30000 which corresponds to 30 seconds.
+186 
+187      <p>Setting this option to zero turns off reconnection
+188      capability.
+189    @param delay milliseconds to wait or zero to not reconnect.
+190    */
+191   public void setReconnectionDelay(final int delay) {
+192     int oldValue = this.reconnectionDelay;
+193     this.reconnectionDelay = delay;
+194     firePropertyChange("reconnectionDelay", oldValue, this.reconnectionDelay);
+195   }
+196 
+197   /***
+198      Returns value of the <b>ReconnectionDelay</b> option.
+199    @return value of reconnection delay option.
+200    */
+201   public int getReconnectionDelay() {
+202     return reconnectionDelay;
+203   }
+204 
+205   /***
+206    * Returns true if the receiver is the same class and they are
+207    * configured for the same properties, and super class also considers
+208    * them to be equivalent. This is used by PluginRegistry when determining
+209    * if the a similarly configured receiver is being started.
+210    *
+211    * @param testPlugin The plugin to test equivalency against.
+212    * @return boolean True if the testPlugin is equivalent to this plugin.
+213    */
+214   public boolean isEquivalent(final Plugin testPlugin) {
+215     if (testPlugin != null && testPlugin instanceof SocketHubReceiver) {
+216       SocketHubReceiver sReceiver = (SocketHubReceiver) testPlugin;
+217 
+218       return (port == sReceiver.getPort()
+219               && host.equals(sReceiver.getHost())
+220               && reconnectionDelay == sReceiver.getReconnectionDelay()
+221               && super.isEquivalent(testPlugin));
+222     }
+223     return false;
+224   }
+225 
+226   /***
+227     Returns true if this receiver is active.
+228    @return true if receiver is active
+229    */
+230   public synchronized boolean isActive() {
+231     return active;
+232   }
+233 
+234   /***
+235     Sets the flag to indicate if receiver is active or not.
+236    @param b new value
+237    */
+238   protected synchronized void setActive(final boolean b) {
+239     active = b;
+240   }
+241 
+242   /***
+243     Starts the SocketReceiver with the current options. */
+244   public void activateOptions() {
+245     if (!isActive()) {
+246       setActive(true);
+247       fireConnector(false);
+248     }
+249   }
+250 
+251   /***
+252     Called when the receiver should be stopped. Closes the socket */
+253   public synchronized void shutdown() {
+254     // mark this as no longer running
+255     active = false;
+256 
+257     // close the socket
+258     try {
+259       if (socket != null) {
+260         socket.close();
+261       }
+262     } catch (Exception e) {
+263       // ignore for now
+264     }
+265     socket = null;
+266 
+267     // stop the connector
+268     if (connector != null) {
+269       connector.interrupted = true;
+270       connector = null;  // allow gc
+271     }
+272   }
+273 
+274   /***
+275     Listen for a socketClosedEvent from the SocketNode. Reopen the
+276     socket if this receiver is still active.
+277    @param e exception not used.
+278    */
+279   public void socketClosedEvent(final Exception e) {
+280     // we clear the connector object here
+281     // so that it actually does reconnect if the
+282     // remote socket dies.
+283     connector = null;
+284     fireConnector(true);
+285   }
+286 
+287     /***
+288      * Fire connectors.
+289      * @param isReconnect true if reconnect.
+290      */
+291   private synchronized void fireConnector(final boolean isReconnect) {
+292     if (active && connector == null) {
+293       getLogger().debug("Starting a new connector thread.");
+294       connector = new Connector(isReconnect);
+295       connector.setDaemon(true);
+296       connector.setPriority(Thread.MIN_PRIORITY);
+297       connector.start();
+298     }
+299   }
+300 
+301     /***
+302      * Set socket.
+303      * @param newSocket new value for socket.
+304      */
+305   private synchronized void setSocket(final Socket newSocket) {
+306     connector = null;
+307     socket = newSocket;
+308     SocketNode13 node = new SocketNode13(socket, this);
+309     node.addSocketNodeEventListener(this);
+310 
+311     synchronized (listenerList) {
+312         for (Iterator iter = listenerList.iterator(); iter.hasNext();) {
+313             SocketNodeEventListener listener =
+314                     (SocketNodeEventListener) iter.next();
+315             node.addSocketNodeEventListener(listener);
+316         }
+317     }
+318     new Thread(node).start();
+319   }
+320 
+321   /***
+322    The Connector will reconnect when the server becomes available
+323    again.  It does this by attempting to open a new connection every
+324    <code>reconnectionDelay</code> milliseconds.
+325 
+326    <p>It stops trying whenever a connection is established. It will
+327    restart to try reconnect to the server when previpously open
+328    connection is droppped.
+329 
+330    @author  Ceki G&uuml;lc&uuml;
+331    */
+332   private final class Connector extends Thread {
+333 
+334       /***
+335        * Interruption status.
+336        */
+337     boolean interrupted = false;
+338       /***
+339        * If true, then delay on next iteration.
+340        */
+341     boolean doDelay;
+342 
+343       /***
+344        * Create new instance.
+345        * @param isReconnect true if reconnecting.
+346        */
+347     public Connector(final boolean isReconnect) {
+348       super();
+349       doDelay = isReconnect;
+350     }
+351 
+352       /***
+353        * Attempt to connect until interrupted.
+354        */
+355     public void run() {
+356       while (!interrupted) {
+357         try {
+358           if (doDelay) {
+359             getLogger().debug("waiting for " + reconnectionDelay
+360               + " milliseconds before reconnecting.");
+361             sleep(reconnectionDelay);
+362           }
+363           doDelay = true;
+364           getLogger().debug("Attempting connection to " + host);
+365           Socket s = new Socket(host, port);
+366           setSocket(s);
+367           getLogger().debug(
+368                   "Connection established. Exiting connector thread.");
+369           break;
+370         } catch (InterruptedException e) {
+371           getLogger().debug("Connector interrupted. Leaving loop.");
+372           return;
+373         } catch (java.net.ConnectException e) {
+374           getLogger().debug("Remote host {} refused connection.", host);
+375         } catch (IOException e) {
+376           getLogger().debug("Could not connect to {}. Exception is {}.",
+377                   host, e);
+378         }
+379       }
+380     }
+381   }
+382 
+383     /***
+384      * This method does nothing.
+385      * @param remoteInfo remote info.
+386      */
+387   public void socketOpened(final String remoteInfo) {
+388 
+389     // This method does nothing.
+390   }
+391 }
+
+
+ + Propchange: logging/site/trunk/docs/log4j/companions/receivers/xref/org/apache/log4j/net/SocketHubReceiver.html ------------------------------------------------------------------------------ svn:mime-type = text/html Added: logging/site/trunk/docs/log4j/companions/receivers/xref/org/apache/log4j/net/SocketNode13.html URL: http://svn.apache.org/viewvc/logging/site/trunk/docs/log4j/companions/receivers/xref/org/apache/log4j/net/SocketNode13.html?view=auto&rev=558362 ============================================================================== --- logging/site/trunk/docs/log4j/companions/receivers/xref/org/apache/log4j/net/SocketNode13.html (added) +++ logging/site/trunk/docs/log4j/companions/receivers/xref/org/apache/log4j/net/SocketNode13.html Sat Jul 21 10:50:33 2007 @@ -0,0 +1,288 @@ + + + +SocketNode13 xref + + + +
View Javadoc
+
+1   /*
+2    * Licensed to the Apache Software Foundation (ASF) under one or more
+3    * contributor license agreements.  See the NOTICE file distributed with
+4    * this work for additional information regarding copyright ownership.
+5    * The ASF licenses this file to You under the Apache License, Version 2.0
+6    * (the "License"); you may not use this file except in compliance with
+7    * the License.  You may obtain a copy of the License at
+8    *
+9    *      http://www.apache.org/licenses/LICENSE-2.0
+10   *
+11   * Unless required by applicable law or agreed to in writing, software
+12   * distributed under the License is distributed on an "AS IS" BASIS,
+13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+14   * See the License for the specific language governing permissions and
+15   * limitations under the License.
+16   */
+17  
+18  package org.apache.log4j.net;
+19  
+20  import java.io.BufferedInputStream;
+21  import java.io.IOException;
+22  import java.io.ObjectInputStream;
+23  import java.net.Socket;
+24  import java.util.ArrayList;
+25  import java.util.Collections;
+26  import java.util.Iterator;
+27  import java.util.List;
+28  
+29  import org.apache.log4j.Logger;
+30  import org.apache.log4j.plugins.Pauseable;
+31  import org.apache.log4j.plugins.Receiver;
+32  import org.apache.log4j.spi.ComponentBase;
+33  import org.apache.log4j.spi.LoggerRepository;
+34  import org.apache.log4j.spi.LoggingEvent;
+35  
+36  
+37  // Contributors:  Moses Hohman <mmhohman@rainbow.uchicago.edu>
+38  
+39  /***
+40     Read {@link LoggingEvent} objects sent from a remote client using
+41     Sockets (TCP). These logging events are logged according to local
+42     policy, as if they were generated locally.
+43  
+44     <p>For example, the socket node might decide to log events to a
+45     local file and also resent them to a second socket node.
+46  
+47      Implementation lifted from org.apache.log4j.net.SocketNode
+48      in log4j 1.3 and renamed to prevent collision with
+49      log4j 1.2 implementation.
+50  
+51      @author  Ceki G&uuml;lc&uuml;
+52      @author  Paul Smith (psmith@apache.org)
+53  
+54  
+55  */
+56  public class SocketNode13 extends ComponentBase implements Runnable, Pauseable {
+57  
+58      /***
+59       * Paused state.
+60       */
+61    private boolean paused;
+62      /***
+63       * Socket.
+64       */
+65    private Socket socket;
+66      /***
+67       * Receiver.
+68       */
+69    private Receiver receiver;
+70      /***
+71       * List of listeners.
+72       */
+73    private List listenerList = Collections.synchronizedList(new ArrayList());
+74  
+75  
+76  
+77    /***
+78      Constructor for socket and logger repository.
+79     @param s socket
+80     @param hierarchy logger repository
+81     */
+82    public SocketNode13(final Socket s,
+83                      final LoggerRepository hierarchy) {
+84      super();
+85      this.socket = s;
+86      this.repository = hierarchy;
+87    }
+88  
+89    /***
+90      Constructor for socket and receiver.
+91     @param s socket
+92     @param r receiver
+93     */
+94    public SocketNode13(final Socket s, final Receiver r) {
+95      super();
+96      this.socket = s;
+97      this.receiver = r;
+98    }
+99  
+100   /***
+101    * Set the event listener on this node.
+102    *
+103    * @deprecated Now supports mutliple listeners, this method
+104    * simply invokes the removeSocketNodeEventListener() to remove
+105    * the listener, and then readds it.
+106    * @param l listener
+107    */
+108   public void setListener(final SocketNodeEventListener l) {
+109     removeSocketNodeEventListener(l);
+110     addSocketNodeEventListener(l);
+111   }
+112 
+113   /***
+114    * Adds the listener to the list of listeners to be notified of the
+115    * respective event.
+116    * @param listener the listener to add to the list
+117    */
+118   public void addSocketNodeEventListener(
+119           final SocketNodeEventListener listener) {
+120     listenerList.add(listener);
+121   }
+122 
+123   /***
+124    * Removes the registered Listener from this instances list of
+125    * listeners.  If the listener has not been registered, then invoking
+126    * this method has no effect.
+127    *
+128    * @param listener the SocketNodeEventListener to remove
+129    */
+130   public void removeSocketNodeEventListener(
+131           final SocketNodeEventListener listener) {
+132     listenerList.remove(listener);
+133   }
+134 
+135 
+136     /***
+137      * Deserialize events from socket until interrupted.
+138      */
+139   public void run() {
+140     LoggingEvent event;
+141     Logger remoteLogger;
+142     Exception listenerException = null;
+143     ObjectInputStream ois = null;
+144 
+145     try {
+146       ois =
+147         new ObjectInputStream(
+148           new BufferedInputStream(socket.getInputStream()));
+149     } catch (Exception e) {
+150       ois = null;
+151       listenerException = e;
+152       getLogger().error("Exception opening ObjectInputStream to " + socket, e);
+153     }
+154 
+155     if (ois != null) {
+156       String remoteInfo =
+157         socket.getInetAddress().getHostName() + ":" + socket.getPort();
+158 
+159       /***
+160        * notify the listener that the socket has been
+161        * opened and this SocketNode is ready and waiting
+162        */
+163       fireSocketOpened(remoteInfo);
+164 
+165       try {
+166         while (true) {
+167           // read an event from the wire
+168           event = (LoggingEvent) ois.readObject();
+169 
+170           // store the known remote info in an event property
+171           event.setProperty("log4j.remoteSourceInfo", remoteInfo);
+172 
+173           // if configured with a receiver, tell it to post the event
+174           if (!isPaused()) {
+175             if ((receiver != null)) {
+176               receiver.doPost(event);
+177 
+178               // else post it via the hierarchy
+179             } else {
+180               // get a logger from the hierarchy. The name of the logger
+181               // is taken to be the name contained in the event.
+182               remoteLogger = repository.getLogger(event.getLoggerName());
+183 
+184               //event.logger = remoteLogger;
+185               // apply the logger-level filter
+186               if (event
+187                 .getLevel()
+188                 .isGreaterOrEqual(remoteLogger.getEffectiveLevel())) {
+189                 // finally log the event as if was generated locally
+190                 remoteLogger.callAppenders(event);
+191               }
+192             }
+193           } else {
+194             //we simply discard this event.
+195           }
+196         }
+197       } catch (java.io.EOFException e) {
+198         getLogger().info("Caught java.io.EOFException closing connection.");
+199         listenerException = e;
+200       } catch (java.net.SocketException e) {
+201         getLogger().info("Caught java.net.SocketException closing connection.");
+202         listenerException = e;
+203       } catch (IOException e) {
+204         getLogger().info("Caught java.io.IOException: " + e);
+205         getLogger().info("Closing connection.");
+206         listenerException = e;
+207       } catch (Exception e) {
+208         getLogger().error("Unexpected exception. Closing connection.", e);
+209         listenerException = e;
+210       }
+211     }
+212 
+213     // close the socket
+214     try {
+215       if (ois != null) {
+216         ois.close();
+217       }
+218     } catch (Exception e) {
+219       //getLogger().info("Could not close connection.", e);
+220     }
+221 
+222     // send event to listener, if configured
+223     if (listenerList.size() > 0) {
+224       fireSocketClosedEvent(listenerException);
+225     }
+226   }
+227 
+228   /***
+229    * Notifies all registered listeners regarding the closing of the Socket.
+230    * @param listenerException listener exception
+231    */
+232   private void fireSocketClosedEvent(final Exception listenerException) {
+233     synchronized (listenerList) {
+234         for (Iterator iter = listenerList.iterator(); iter.hasNext();) {
+235             SocketNodeEventListener snel =
+236                     (SocketNodeEventListener) iter.next();
+237             if (snel != null) {
+238                 snel.socketClosedEvent(listenerException);
+239             }
+240         }
+241     }
+242   }
+243 
+244   /***
+245    * Notifies all registered listeners regarding the opening of a Socket.
+246    * @param remoteInfo remote info
+247    */
+248   private void fireSocketOpened(final String remoteInfo) {
+249     synchronized (listenerList) {
+250         for (Iterator iter = listenerList.iterator(); iter.hasNext();) {
+251             SocketNodeEventListener snel =
+252                     (SocketNodeEventListener) iter.next();
+253             if (snel != null) {
+254                 snel.socketOpened(remoteInfo);
+255             }
+256         }
+257     }
+258   }
+259 
+260     /***
+261      * Sets if node is paused.
+262      * @param b new value
+263      */
+264   public void setPaused(final boolean b) {
+265     this.paused = b;
+266   }
+267 
+268     /***
+269      * Get if node is paused.
+270      * @return true if pause.
+271      */
+272   public boolean isPaused() {
+273     return this.paused;
+274   }
+275 }
+
+
+ + Propchange: logging/site/trunk/docs/log4j/companions/receivers/xref/org/apache/log4j/net/SocketNode13.html ------------------------------------------------------------------------------ svn:mime-type = text/html Added: logging/site/trunk/docs/log4j/companions/receivers/xref/org/apache/log4j/net/SocketNodeEventListener.html URL: http://svn.apache.org/viewvc/logging/site/trunk/docs/log4j/companions/receivers/xref/org/apache/log4j/net/SocketNodeEventListener.html?view=auto&rev=558362 ============================================================================== --- logging/site/trunk/docs/log4j/companions/receivers/xref/org/apache/log4j/net/SocketNodeEventListener.html (added) +++ logging/site/trunk/docs/log4j/companions/receivers/xref/org/apache/log4j/net/SocketNodeEventListener.html Sat Jul 21 10:50:33 2007 @@ -0,0 +1,57 @@ + + + +SocketNodeEventListener xref + + + +
View Javadoc
+
+1   /*
+2    * Licensed to the Apache Software Foundation (ASF) under one or more
+3    * contributor license agreements.  See the NOTICE file distributed with
+4    * this work for additional information regarding copyright ownership.
+5    * The ASF licenses this file to You under the Apache License, Version 2.0
+6    * (the "License"); you may not use this file except in compliance with
+7    * the License.  You may obtain a copy of the License at
+8    *
+9    *      http://www.apache.org/licenses/LICENSE-2.0
+10   *
+11   * Unless required by applicable law or agreed to in writing, software
+12   * distributed under the License is distributed on an "AS IS" BASIS,
+13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+14   * See the License for the specific language governing permissions and
+15   * limitations under the License.
+16   */
+17  
+18  package org.apache.log4j.net;
+19  
+20  import java.util.EventListener;
+21  
+22  /***
+23    Interface used to listen for {@link SocketNode} related
+24    events. Clients register an instance of the interface and the
+25    instance is called back when the various events occur.
+26  
+27    @author Mark Womack
+28    @author Paul Smith (psmith@apache.org)
+29    @since 1.3
+30  */
+31  public interface SocketNodeEventListener extends EventListener {
+32  
+33    /***
+34     * Called when the SocketNode is created and begins awaiting data.
+35     *  @param remoteInfo remote info
+36     */
+37    void socketOpened(String remoteInfo);
+38  
+39    /***
+40      Called when the socket the node was given has been closed.
+41      @param e exception
+42     */
+43    void socketClosedEvent(Exception e);
+44  }
+
+
+ + Propchange: logging/site/trunk/docs/log4j/companions/receivers/xref/org/apache/log4j/net/SocketNodeEventListener.html ------------------------------------------------------------------------------ svn:mime-type = text/html