Commit 6a51cf2b authored by Loïc Albertin's avatar Loïc Albertin
Browse files

#1041: ServletContainerInitializer registration through OSGi for Tomcat is broken

  * Fix + tests
parent edb53711
......@@ -40,5 +40,6 @@
<modules>
<module>osgi-datasource</module>
<module>sci</module>
</modules>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>jonas-itests-applications-osgi-sci</artifactId>
<groupId>org.ow2.jonas</groupId>
<version>6.0.0-M1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>JOnAS :: iTests :: Applications :: OSGi :: ServletContainerInitializer :: API</name>
<artifactId>jonas-itests-applications-osgi-sci-api</artifactId>
<packaging>jar</packaging>
</project>
\ No newline at end of file
/*
* JOnAS: Java(TM) Open Application Server
* Copyright (C) 2014 Bull S.A.S.
* Contact: jonas-team@ow2.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
* --------------------------------------------------------------------------
* $Id$
* --------------------------------------------------------------------------
*/
package org.ow2.jonas.itests.osgi.sci.api;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* A {@code Handler} is an annotation used to trigger a ServletContainerInitializer
*
* @author Loic Albertin
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Handler {
}
/*
* JOnAS: Java(TM) Open Application Server
* Copyright (C) 2014 Bull S.A.S.
* Contact: jonas-team@ow2.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
* --------------------------------------------------------------------------
* $Id$
* --------------------------------------------------------------------------
*/
package org.ow2.jonas.itests.osgi.sci.api;
/**
* A {@code IHandler} is a an interface used to trigger a ServletContainerInitializer
*
* @author Loic Albertin
*/
public interface IHandler {
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>jonas-itests-applications-osgi-sci</artifactId>
<groupId>org.ow2.jonas</groupId>
<version>6.0.0-M1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>JOnAS :: iTests :: Applications :: OSGi :: ServletContainerInitializer :: Bundle</name>
<artifactId>jonas-itests-applications-osgi-sci-bundle</artifactId>
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>org.ow2.jonas</groupId>
<artifactId>jonas-itests-applications-osgi-sci-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.ow2.spec.ee</groupId>
<artifactId>ow2-servlet-3.0-spec</artifactId>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.ipojo.annotations</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
/*
* JOnAS: Java(TM) Open Application Server
* Copyright (C) 2014 Bull S.A.S.
* Contact: jonas-team@ow2.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
* --------------------------------------------------------------------------
* $Id$
* --------------------------------------------------------------------------
*/
package org.ow2.jonas.itests.osgi.sci.internal;
import javax.servlet.annotation.HandlesTypes;
import org.ow2.jonas.itests.osgi.sci.api.Handler;
/**
* A {@code JServletContainerInitializer} is a {@link javax.servlet.ServletContainerInitializer} that handles types annotated with the
* {@link org.ow2.jonas.itests.osgi.sci.api.Handler} annotation.
*
* @author Loic Albertin
*/
@HandlesTypes({Handler.class})
public class AnnotationServletContainerInitializer extends JServletContainerInitializer {
}
/*
* JOnAS: Java(TM) Open Application Server
* Copyright (C) 2014 Bull S.A.S.
* Contact: jonas-team@ow2.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
* --------------------------------------------------------------------------
* $Id$
* --------------------------------------------------------------------------
*/
package org.ow2.jonas.itests.osgi.sci.internal;
import javax.servlet.annotation.HandlesTypes;
import org.ow2.jonas.itests.osgi.sci.api.IHandler;
/**
* A {@code JServletContainerInitializer} is a {@link javax.servlet.ServletContainerInitializer} that handles types inheriting
* {@link org.ow2.jonas.itests.osgi.sci.api.IHandler}.
*
* @author Loic Albertin
*/
@HandlesTypes({IHandler.class})
public class InheritedServletContainerInitializer extends JServletContainerInitializer {
}
/*
* JOnAS: Java(TM) Open Application Server
* Copyright (C) 2014 Bull S.A.S.
* Contact: jonas-team@ow2.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
* --------------------------------------------------------------------------
* $Id$
* --------------------------------------------------------------------------
*/
package org.ow2.jonas.itests.osgi.sci.internal;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
/**
* A {@code JServletContainerInitializer} is a base class for our {@link javax.servlet.ServletContainerInitializer}s that tracks classes
* handled by those initializers
*
* @author Loic Albertin
*/
public abstract class JServletContainerInitializer implements ServletContainerInitializer {
@Override
public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException {
if (classes != null) {
Set<String> initBy = (Set<String>) servletContext.getAttribute("InitializedBy");
if (initBy == null) {
initBy = new HashSet<String>();
servletContext.setAttribute("InitializedBy", initBy);
}
StringBuilder builder = new StringBuilder(this.getClass().getName());
builder.append(" for classes: ");
for (Class<?> aClass : classes) {
builder.append(aClass.getName()).append(" ");
}
initBy.add(builder.toString());
}
}
}
/*
* JOnAS: Java(TM) Open Application Server
* Copyright (C) 2014 Bull S.A.S.
* Contact: jonas-team@ow2.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
* --------------------------------------------------------------------------
* $Id$
* --------------------------------------------------------------------------
*/
package org.ow2.jonas.itests.osgi.sci.internal;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.ServletContainerInitializer;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Validate;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
/**
* A {@code Startup} is a iPOJO component used to register the {@link javax.servlet.ServletContainerInitializer}s
*
* @author Loic Albertin
*/
@Component
@Instantiate
public class Startup {
private BundleContext bundleContext;
private Set<ServiceRegistration<ServletContainerInitializer>> registrations =
new HashSet<ServiceRegistration<ServletContainerInitializer>>();
/**
* @param bundleContext The {@link BundleContext} used to register the
* {@link org.ow2.jonas.itests.osgi.sci.internal.JServletContainerInitializer}
*/
public Startup(BundleContext bundleContext) {
this.bundleContext = bundleContext;
}
@Validate
private void startup() {
registrations.add(
bundleContext.registerService(ServletContainerInitializer.class,
new AnnotationServletContainerInitializer(), null)
);
registrations.add(
bundleContext.registerService(ServletContainerInitializer.class,
new InheritedServletContainerInitializer(), null)
);
}
@Invalidate
private void shutdown() {
for (ServiceRegistration<ServletContainerInitializer> registration : registrations) {
registration.unregister();
}
registrations.clear();
}
}
# ---------------------------------------------------------------------------
# JOnAS: Java(TM) Open Application Server
# Copyright (C) 2011 bull S.A.S.
# Contact: jonas-team@ow2.org
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
# ---------------------------------------------------------------------------
# $Id$
# ---------------------------------------------------------------------------
# Wrap API
Export-Package: org.ow2.jonas.itests.osgi.sci.api
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>jonas-itests-applications-osgi</artifactId>
<groupId>org.ow2.jonas</groupId>
<version>6.0.0-M1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jonas-itests-applications-osgi-sci</artifactId>
<name>JOnAS :: iTests :: Applications :: OSGi :: ServletContainerInitializer</name>
<packaging>pom</packaging>
<modules>
<module>api</module>
<module>bundle</module>
<module>war</module>
</modules>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>jonas-itests-applications-osgi-sci</artifactId>
<groupId>org.ow2.jonas</groupId>
<version>6.0.0-M1-SNAPSHOT</version>
<relativePath>../../sci/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>JOnAS :: iTests :: Applications :: OSGi :: ServletContainerInitializer :: WAR</name>
<artifactId>jonas-itests-applications-osgi-sci-war</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.ow2.jonas</groupId>
<artifactId>jonas-itests-applications-osgi-sci-api</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.ow2.spec.ee</groupId>
<artifactId>ow2-servlet-3.0-spec</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
/*
* JOnAS: Java(TM) Open Application Server
* Copyright (C) 2014 Bull S.A.S.
* Contact: jonas-team@ow2.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
* --------------------------------------------------------------------------
* $Id$
* --------------------------------------------------------------------------
*/
package org.ow2.jonas.itests.osgi.sci.war;
import org.ow2.jonas.itests.osgi.sci.api.Handler;
/**
* A {@code AnnotatedModel} is a POJO annotated with an {@link org.ow2.jonas.itests.osgi.sci.api.Handler} annotation. It should appear
* in the "InitializedBy" attribute of the {@link javax.servlet.ServletContext}
*
* @author Loic Albertin
*/
@Handler
public class AnnotatedModel {
}
/*
* JOnAS: Java(TM) Open Application Server
* Copyright (C) 2014 Bull S.A.S.
* Contact: jonas-team@ow2.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
* --------------------------------------------------------------------------
* $Id$
* --------------------------------------------------------------------------
*/
package org.ow2.jonas.itests.osgi.sci.war;
import java.io.IOException;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.ow2.jonas.itests.osgi.sci.api.IHandler;
/**
* A {@code SCIServlet} is an {@link javax.servlet.http.HttpServlet} that implements {@link org.ow2.jonas.itests.osgi.sci.api.IHandler}.
* It should appear in the "InitializedBy" attribute of the {@link javax.servlet.ServletContext}.
*
* @author Loic Albertin
*/
public class SCIServlet extends HttpServlet implements IHandler {
private static final long serialVersionUID = 4412234643128656791L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/plain");
Set<String> initBy = (Set<String>)
getServletContext().getAttribute("InitializedBy");
ServletOutputStream sos = resp.getOutputStream();
if (initBy == null || initBy.isEmpty()) {
sos.println("Servlet Context not initialized by the OSGi ServletContainerInitializer.");
} else {
for (String i : initBy) {
sos.println("InitializedBy: " + i);
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!--
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- JOnAS: Java(TM) Open Application Server
- Copyright (C) 2014 Bull S.A.S.
- Contact: jonas-team@ow2.org
-
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2.1 of the License, or any later version.
-
- This library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public
- License along with this library; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- USA
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -