Skip to content

Commit

Permalink
Merge branch 'project' into central
Browse files Browse the repository at this point in the history
* project:
  Fixed jetty startup to listen on all hosts for trusted and user connectors
  Adapted Felix Http Jetty service activation to use jetty.xml rather than OSGi properties as our deployment is generally more complex than can be handled by the standard Felix bundle (we need 3 connectiors on differnt ports)
  Set default to on MessageBucket URL to something that works on an IPv6 machine. 
  Fixed rather inefficient stream handling.
  • Loading branch information
ieb committed Mar 7, 2011
2 parents 984ab57 + 337dad1 commit 25e5a51
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 20 deletions.
@@ -0,0 +1,25 @@
package org.apache.felix.http.jetty.internal;

import org.apache.felix.http.base.internal.AbstractHttpActivator;

public class NakamuraActivator extends AbstractHttpActivator {

private NakamuraJettyService jetty;

protected void doStart()
throws Exception
{
super.doStart();
this.jetty = new NakamuraJettyService(getBundleContext(), getDispatcherServlet(), getEventDispatcher(),
getHttpServiceController());
this.jetty.start();
}

protected void doStop()
throws Exception
{
this.jetty.stop();
super.doStop();
}

}
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.felix.http.jetty.internal;

import java.util.Dictionary;

import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.ManagedService;

public class NakamuraJettyManagedService implements ServiceFactory
{

private final NakamuraJettyService jettyService;

NakamuraJettyManagedService(final NakamuraJettyService jettyService)
{
this.jettyService = jettyService;
}

public Object getService(Bundle bundle, ServiceRegistration registration)
{
return new ManagedService()
{
@SuppressWarnings("rawtypes")
public void updated(Dictionary properties)
{
jettyService.updated(properties);
}
};
}

public void ungetService(Bundle bundle, ServiceRegistration registration, Object service)
{
// just have the reference dropped, nothing to cleanup
}

}
@@ -0,0 +1,174 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.felix.http.jetty.internal;

import org.apache.felix.http.base.internal.DispatcherServlet;
import org.apache.felix.http.base.internal.EventDispatcher;
import org.apache.felix.http.base.internal.HttpServiceController;
import org.apache.felix.http.base.internal.logger.SystemLogger;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.security.HashUserRealm;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;
import org.mortbay.xml.XmlConfiguration;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;

import java.io.InputStream;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Properties;

public final class NakamuraJettyService
implements Runnable
{
/** PID for configuration of the HTTP service. */
private static final String PID = "org.apache.felix.http";

private final BundleContext context;
private boolean running;
private Thread thread;
private ServiceRegistration configServiceReg;
private Server server;
private DispatcherServlet dispatcher;
private EventDispatcher eventDispatcher;
private final HttpServiceController controller;

public NakamuraJettyService(BundleContext context, DispatcherServlet dispatcher, EventDispatcher eventDispatcher,
HttpServiceController controller)
{
this.context = context;
this.dispatcher = dispatcher;
this.eventDispatcher = eventDispatcher;
this.controller = controller;
}

public void start()
throws Exception
{
JettyLogger.init();

Properties props = new Properties();
props.put(Constants.SERVICE_PID, PID);
this.configServiceReg = this.context.registerService("org.osgi.service.cm.ManagedService",
new NakamuraJettyManagedService(this), props);

this.thread = new Thread(this, "Jetty HTTP Service");
this.thread.start();
}

public void stop()
throws Exception
{
if (this.configServiceReg != null) {
this.configServiceReg.unregister();
}

this.running = false;
this.thread.interrupt();

try {
this.thread.join(3000);
} catch (InterruptedException e) {
// Do nothing
}
}

private void publishServiceProperties()
{
Hashtable<String, Object> props = new Hashtable<String, Object>();
this.controller.setProperties(props);
}

@SuppressWarnings("rawtypes")
public void updated(Dictionary props)
{
if (this.running && (this.thread != null)) {
this.thread.interrupt();
}
}

private void startJetty()
{
try {
initializeJetty();
} catch (Exception e) {
SystemLogger.error("Exception while initializing Jetty.", e);
}
}

private void stopJetty()
{
if (this.server != null)
{
try
{
this.server.stop();
this.server = null;
}
catch (Exception e)
{
SystemLogger.error("Exception while stopping Jetty.", e);
}
}
}

private void initializeJetty()
throws Exception
{
StringBuffer message = new StringBuffer("Started jetty ").append(Server.getVersion()).append(" at port(s)");
HashUserRealm realm = new HashUserRealm("OSGi HTTP Service Realm");
this.server = new Server();
this.server.addUserRealm(realm);
InputStream configStream = this.getClass().getClassLoader().getResourceAsStream("jetty.xml");
XmlConfiguration configuration = new XmlConfiguration(configStream);
configuration.configure(server);
configStream.close();


Context context = new Context(this.server, "/", Context.SESSIONS);
context.addEventListener(eventDispatcher);
context.getSessionHandler().addEventListener(eventDispatcher);
context.addServlet(new ServletHolder(this.dispatcher), "/*");

this.server.start();
SystemLogger.info(message.toString());
publishServiceProperties();
}


public void run()
{
this.running = true;
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());

while (this.running) {
startJetty();

synchronized (this) {
try {
wait();
} catch (InterruptedException e) {
// we will definitely be interrupted
}
}

stopJetty();
}
}
}
18 changes: 10 additions & 8 deletions bundles/jetty-config/src/main/resources/META-INF/MANIFEST.MF
Expand Up @@ -6,21 +6,23 @@ Export-Package: org.apache.felix.http.api;uses:="javax.servlet,org.osg
ion="2.5",javax.servlet.http;uses:="javax.servlet";version="2.5",org.
sakaiproject.nakamura.api.servlet;version="0.10",org.mortbay.util.aja
x;version="6.1.22"
Built-By: fmeschbe
Built-By: ieb
Tool: Bnd-0.0.357
Bundle-Name: Apache Felix Http Jetty
Created-By: Apache Maven Bundle Plugin
Bundle-Vendor: The Apache Software Foundation
Bundle-Name: Nakamura Http Jetty
Created-By: Hand
Bundle-Vendor: The Sakai Software Foundation
DynamicImport-Package: org.osgi.service.cm;version=1.2
Build-Jdk: 1.6.0_13
Bundle-Version: 2.2.0
Bnd-LastModified: 1296053619491
Bundle-Activator: org.apache.felix.http.jetty.internal.JettyActivator
Bundle-Activator: org.apache.felix.http.jetty.internal.NakamuraActivat
or
Service-Component: OSGI-INF/serviceComponents.xml
Bundle-ManifestVersion: 2
Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt
Bundle-Description: Apache Felix is an OSGi implementation.
Bundle-DocURL: http://www.apache.org/
Bundle-Description: This bundle is based on the Apache Felix Http Jett
y bundle with some small modifications for Nakamura. Apache Felix is
an OSGi implementation.
Bundle-DocURL: http://www.sakaiproject.org/
Bundle-SymbolicName: org.apache.felix.http.jetty
Import-Package: javax.net.ssl;resolution:=optional,javax.security.cert
;resolution:=optional,javax.servlet;resolution:=optional;version="2.5
Expand Down
19 changes: 16 additions & 3 deletions bundles/jetty-config/src/main/resources/jetty.xml
Expand Up @@ -33,13 +33,15 @@
</Set>



<!-- =========================================================== -->
<!-- Add an AJP listener on port 8009 -->
<!-- Add an Http listener on port 8080, trusted operations to the core server -->
<!-- =========================================================== -->
<Call name="addConnector">
<Arg>
<New class="org.mortbay.jetty.ajp.Ajp13SocketConnector">
<Set name="port"><SystemProperty name="org.mortbay.jetty.ajp.port" default="8009"/></Set>
<New class="org.mortbay.jetty.nio.SelectChannelConnector">
<Set name="host"><SystemProperty name="org.sakaiproject.nakamura.core.host" default="0.0.0.0" /></Set>
<Set name="port"><SystemProperty name="org.sakaiproject.nakamura.core.port" default="8080"/></Set>
</New>
</Arg>
</Call>
Expand All @@ -55,5 +57,16 @@
</New>
</Arg>
</Call>
<!-- =========================================================== -->
<!-- Add an Http listener on port 8082 for delivering User Content -->
<!-- =========================================================== -->
<Call name="addConnector">
<Arg>
<New class="org.mortbay.jetty.nio.SelectChannelConnector">
<Set name="host"><SystemProperty name="org.sakaiproject.nakamura.content.host" default="0.0.0.0" /></Set>
<Set name="port"><SystemProperty name="org.sakaiproject.nakamura.content.port" default="8082"/></Set>
</New>
</Arg>
</Call>

</Configure>
Expand Up @@ -30,7 +30,7 @@
@Service(value=MessageBucketService.class)
public class UntrustedMessageBucketServiceImpl implements MessageBucketService {

private static final String DEFAULT_URL_PATTERN = "{0}://{1}:{2}/system/uievent/default?token={3}&server={6}&user={7}";
private static final String DEFAULT_URL_PATTERN = "http://localhost:8080/system/uievent/default?token={3}&server={6}&user={7}";
private static final String BUCKETURLPATTERN_CONFIG = "bucketurlpattern";
private String sharedSecret;
private Map<String, MessageBucket> messageBuckets = new ConcurrentHashMap<String, MessageBucket>();
Expand Down
Expand Up @@ -109,22 +109,28 @@ public <Type> Type adaptTo(Class<Type> type) {
retval = (Type) session;
} else if (type == InputStream.class) {
try {
StringWriter sw = new StringWriter();
ExtendedJSONWriter writer = new ExtendedJSONWriter(sw);
writer.valueMap(content.getProperties());
String s = sw.toString();
ByteArrayInputStream bais = new ByteArrayInputStream(s.getBytes(UTF_8));
retval = (Type) bais;
retval = (Type) contentManager.getInputStream(content.getPath());
} catch (JSONException e) {
logger.error(e.getMessage(), e);
} catch (IOException e) {
logger.error(e.getMessage(), e);
} catch (AccessDeniedException e) {
logger.error(e.getMessage(), e);
} catch (StorageClientException e) {
logger.error(e.getMessage(), e);
}
if ( retval == null ) {
try {
StringWriter sw = new StringWriter();
ExtendedJSONWriter writer = new ExtendedJSONWriter(sw);
writer.valueMap(content.getProperties());
String s = sw.toString();
ByteArrayInputStream bais = new ByteArrayInputStream(s.getBytes(UTF_8));
retval = (Type) bais;
} catch (JSONException e) {
logger.error(e.getMessage(), e);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
} else if (type == ValueMap.class) {
// TODO BL120 logic in SparseValueMapDecorator may need another look
SparseValueMapDecorator vm = new SparseValueMapDecorator(content.getProperties());
Expand Down

0 comments on commit 25e5a51

Please sign in to comment.