Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

NEXUS-5118: Fixing the initial imple

The initial implementation was conceived okay, but
there was a problem with Injection happening as the class
loaded up had annotations thrown away by classloader.

Reason for that is that in bundle layout the appender
is on Jetty classloader level (parent of Nexus webapp)
and that CL does not have @Inject. Hence, the fix was to
introduce "special" case for injection when ForwaringAppender
was met, simply doing manual injection on it.
  • Loading branch information...
commit 9b6bffbfeef248cbf4f4c3cdfeb58fc6678bd7f9 1 parent 6d10eba
@cstamas cstamas authored
View
13 nexus/nexus-logging-extras-appender/pom.xml
@@ -26,12 +26,6 @@
<name>Nexus : Logging Extras - Appender</name>
<dependencies>
- <!-- We have components in here -->
- <dependency>
- <groupId>javax.inject</groupId>
- <artifactId>javax.inject</artifactId>
- </dependency>
-
<!-- Logging -->
<dependency>
<groupId>ch.qos.logback</groupId>
@@ -43,13 +37,6 @@
<artifactId>logback-classic</artifactId>
<scope>compile</scope>
</dependency>
-
- <!-- Testing -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit-dep</artifactId>
- <scope>test</scope>
- </dependency>
</dependencies>
</project>
View
40 nexus/nexus-logging-extras-appender/src/main/java/org/sonatype/nexus/logback/ForwardingAppender.java
@@ -12,9 +12,6 @@
*/
package org.sonatype.nexus.logback;
-import javax.inject.Inject;
-import javax.inject.Named;
-
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.UnsynchronizedAppenderBase;
@@ -24,19 +21,50 @@
* @author cstamas
* @since 2.2
*/
-@Named
public class ForwardingAppender
extends UnsynchronizedAppenderBase<ILoggingEvent>
{
- @Inject
private EventTarget target;
+ /**
+ * Default constructor that creates and resets this instance.
+ */
+ public ForwardingAppender()
+ {
+ reset();
+ }
+
@Override
protected void append( final ILoggingEvent event )
{
+ target.onEvent( event );
+ }
+
+ // ==
+
+ /**
+ * Installs or replaces an {@link EventTarget} to have events forwarded to it.
+ *
+ * @param target
+ */
+ public void installEventTarget( final EventTarget target )
+ {
if ( target != null )
{
- target.onEvent( event );
+ this.target = target;
+ }
+ else
+ {
+ reset();
}
}
+
+ /**
+ * Performs a "reset" of this forwarding appender, and previously installed {@link EventTarget} (if any) will stop
+ * receiving events.
+ */
+ public void reset()
+ {
+ this.target = NoopEventTarget.INSTANCE;
+ }
}
View
15 nexus/nexus-logging-extras-appender/src/main/java/org/sonatype/nexus/logback/NoopEventTarget.java
@@ -0,0 +1,15 @@
+package org.sonatype.nexus.logback;
+
+import ch.qos.logback.classic.spi.ILoggingEvent;
+
+public final class NoopEventTarget
+ implements EventTarget
+{
+ final static NoopEventTarget INSTANCE = new NoopEventTarget();
+
+ @Override
+ public void onEvent( final ILoggingEvent event )
+ {
+ // noop
+ }
+}
View
14 nexus/nexus-logging-extras/src/main/java/org/sonatype/nexus/log/internal/LogbackLogManager.java
@@ -50,6 +50,8 @@
import org.sonatype.nexus.log.LogConfiguration;
import org.sonatype.nexus.log.LogConfigurationParticipant;
import org.sonatype.nexus.log.LogManager;
+import org.sonatype.nexus.logback.EventTarget;
+import org.sonatype.nexus.logback.ForwardingAppender;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
@@ -101,6 +103,9 @@
@Requirement
private ApplicationConfiguration applicationConfiguration;
+ @Requirement
+ private EventTarget eventTarget;
+
private ObjectName jmxName;
public LogbackLogManager()
@@ -482,6 +487,15 @@ private void injectAppenders()
{
Appender<ILoggingEvent> ap = it.next();
injector.injectMembers( ap );
+
+ // ForwardingAppender needs some special care: manual injection
+ // since @Inject is not on Jetty classpath (parent of this classpath) and Injector will not do anything
+ // with it since classloader will not pick up annotations on class while loading up the class itself
+ if ( ap instanceof ForwardingAppender )
+ {
+ final ForwardingAppender fap = (ForwardingAppender) ap;
+ fap.installEventTarget( eventTarget );
+ }
}
}
}
View
13 nexus/nexus-logging-extras/src/main/java/org/sonatype/nexus/log/internal/NexusEventSystemEventTarget.java
@@ -12,11 +12,8 @@
*/
package org.sonatype.nexus.log.internal;
-import javax.enterprise.inject.Typed;
-import javax.inject.Inject;
-import javax.inject.Named;
-import javax.inject.Singleton;
-
+import org.codehaus.plexus.component.annotations.Component;
+import org.codehaus.plexus.component.annotations.Requirement;
import org.sonatype.nexus.logback.EventTarget;
import org.sonatype.plexus.appevents.ApplicationEventMulticaster;
@@ -28,13 +25,11 @@
* @author cstamas
* @since 2.2
*/
-@Named
-@Singleton
-@Typed( EventTarget.class )
+@Component( role = EventTarget.class )
public class NexusEventSystemEventTarget
implements EventTarget
{
- @Inject
+ @Requirement
private ApplicationEventMulticaster eventMulticaster;
@Override
View
67 nexus/nexus-rest-api/src/main/java/org/sonatype/nexus/rest/NexusClientDetector.java
@@ -0,0 +1,67 @@
+/**
+ * Sonatype Nexus (TM) Open Source Version
+ * Copyright (c) 2007-2012 Sonatype, Inc.
+ * All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
+ *
+ * This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
+ * which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
+ *
+ * Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
+ * of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
+ * Eclipse Foundation. All other trademarks are the property of their respective owners.
+ */
+package org.sonatype.nexus.rest;
+
+import java.util.List;
+
+import org.restlet.data.CharacterSet;
+import org.restlet.data.MediaType;
+import org.restlet.data.Preference;
+import org.restlet.data.Request;
+
+/**
+ * Helper class to detect Nexus client.
+ *
+ * @author cstamas
+ * @since 2.1
+ */
+public class NexusClientDetector
+{
+ /**
+ * Returns true if client is some sort of "application" (whether standalone or part of some RESTful UI, does not
+ * matter): point is that response is not "rendered" directly to user (like an error page in Browser, but should be
+ * made available in programmatically reusable way). This is how NexusClient behaves: accepts only XML and nothing
+ * else and enforces only UTF8 character encoding and nothing else (client explicitly states this in Accept header).
+ *
+ * @param request
+ * @return
+ */
+ public boolean isNexusClientAlike( final Request request )
+ {
+ final List<Preference<CharacterSet>> acceptedCharacterSets = request.getClientInfo().getAcceptedCharacterSets();
+ final List<Preference<MediaType>> acceptedMediaTypes = request.getClientInfo().getAcceptedMediaTypes();
+ if ( acceptedCharacterSets.size() == 1
+ && CharacterSet.UTF_8.equals( acceptedCharacterSets.get( 0 ).getMetadata() ) )
+ {
+ if ( acceptedMediaTypes.size() == 1
+ && MediaType.APPLICATION_XML.equals( acceptedMediaTypes.get( 0 ).getMetadata(), true ) )
+ {
+ // this is a client
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Returns true if request initiator presents itself as "Nexus-Client" and has all the properties that Nexus client
+ * has.
+ *
+ * @param request
+ * @return
+ */
+ public boolean isNexusClient( final Request request )
+ {
+ return isNexusClientAlike( request ) && request.getClientInfo().getAgent().startsWith( "Nexus-Client/" );
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.