I'm using ServletContextHandler.
Here is the example:
Server server = new Server();
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setResourceBase("C:\Demo");
context.setContextPath("/");
server.setHandler(context);
server.start();
So, As far as my understanding, please correct me if I'm wrong, that files available under "C:\Demo" can be accessed using "localhost:8081/" as resourcebase is set to this location and context path is defined as "/".
So What if we don't set the resource base then to which directory "localhost:8081/" will point?
It wont point to anything.
And you'll also have a tough time configuring things that need a valid javax.servlet.ServletContext
Any servlet or library that needs configuration from the following ServletContext methods would fail without a Base Resource defined.
String ServletContext.getRealPath(String path)
URL ServletContext.getResource(String path)
InputStream ServletContext.getResourceAsStream(String path)
Set<String> ServletContext.getResources(String path)
Related
I have an application, which refers to a MY_ PRODUCT_CONF_DIR/mycustom.properties file which has some key value pairs which needs to be external to the ear, war or jars deployed on my WildFly. Earlier in Jboss 6.1.0. we did it in a tricky way. The jboss 6.1.0, have a collection of URLs, visible to CL loading the server.
For example ( https://repository.jboss.org/org/jboss/jbossas/jboss-as-distribution/6.1.0.Final/, refer to jboss-6.1.0.Final-src\main\src\main\java\org\jboss\Main.java )
// Define a Set URLs to have visible to the CL loading the Server
final Set<URL> urls = new HashSet<URL>();
..........
urls.add(new File(MY_ PRODUCT_CONF_DIR)).toURI().toURL()); // I have added the DIR
.........
// Make a ClassLoader to be used in loading the server
final URL[] urlArray = urls.toArray(new URL[]{});
final ClassLoader loadingCl = new URLClassLoader(urlArray, tccl);
// Load the server
server = JBossASServer.class.cast(ServerFactory.createServer(DEFAULT_AS_SERVER_IMPL_CLASS_NAME, loadingCl));
In my code, I read the properties file from ClassLoader
URLClassLoader ucl = (URLClassLoader) loader;
url = ucl.findResource(propertiesResource);
final InputStream inputStream = url.openStream();
..........
So, is there any option to retain this mechanism? Can I add my CONFIG_DIR in the ModuleClassLoader as a URLClassLoader?
Is there any way to keep the properties file external to the ear/jars and module path?
You can put your properties file in a module and load it that way without having to use a URLClassLoader for getResource or getInputStream . Another way would be to specify the path to your properties files in some system property and then just do a Properties.load(Files.newInputStream(myPath))
I had created a web app with shiro. Now i want to secure aem application with Shiro. I am not able to get where to place the shiro.ini file and how to set EnvironmentLoaderListener and ShiroFilter.
I had tried a lot of things like fetching ini file through bundleContext in activate() method
I want to know where i have to do the shiro configurations in aem?
I had put the shiro file in resource folder and send the shiro ini file in the bundle and then fetched it from inside the bundle:
I had fetched the servlet bundleContext as:
#Activate
public void activate(BundleContext bundleContext) throws IOException {
this.bundleContext = bundleContext;
}
And then used this bundleContext to fetch the url of ini file
URL iniURL = bundleContext.getBundle().getEntry("shiro.ini");
Ini ini = new Ini();
ini.load(iniURL.openStream());
IniSecurityManagerFactory factory = new IniSecurityManagerFactory(ini);
securityManager = factory.getInstance();
This is how i got the securityManager.
And then used the shiro api for further login and logout purpose.
I am trying to access a Datasource specified in the Tomcat server.xml and context.xml by
play.
The playapplication is located as war within the tomcat webapps and also has the Connection in its web.xml specified with:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/Testconnection</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
To see hat the connection is specified I used a jsp to check the configuration:
Context initialContext = new InitialContext();
Context componentBindings = (Context) initialContext.lookup("java:comp/env");
DataSource dataSource = (DataSource) componentBindings.lookup("jdbc/Testconnection");
Connection connection = dataSource.getConnection();
String connectionUrl = connection.getMetaData().getURL();
out.println(connectionUrl);
This showed the exspected result.
When trying to access the database within the play application it always results in:
"could not find datasource for ....: Token not allowed in path expression: '-'" pointing to the line containing DB.withConnection in my play app.
So what is confusing me is that there is not such a token as '-' within my whole code.
I tried to access the following combinations:
DB.withConnection("java:comp/env/jdbc/Testconnection") { imlicit c => .......}
DB.withConnection("java:jdbc/Testconnection") { imlicit c => .......}
DB.withConnection("jdbc/Testconnection") { imlicit c => .......}
Would be great if anybody can provide me with a solution or point me in the right direction.
Thank you very much!
Solution:
So the Solution (thanks to applicius!) could be:
import javax.naming.{Context, InitialContext}
import javax.sql.DataSource
[...]
var conn: java.sql.Connection = (new InitialContext).lookup("java:/comp/env").asInstanceOf[Context].lookup("jdbc/Testconnection").asInstanceOf[DataSource].getConnection
val result: Option[Result] = SQL(queryName).on(("variable","replaceValue")).singleOpt(Result.rowMapper)(conn)
[...]
Play DB API is expecting a Play datasource name (one corresponding to entry in Play application.conf), not a JNDI name.
Either you get connection by your self using regular JNDI lookup and use it in your Play app (having to release it by yourself), or you move/duplicate datasource settings into Play config.
I'm trying to use Apache Shiro (v1.2) in a JavaEE6 web application on a GlassFish v3.1.2 application server.
In GlassFish I've set up a datasource (jdbc/myds) which I have tested to work through JPA2.
I would like to use the same datasource for authenticating users using the Shiro JDBC realm.
I've got the following in shiro.ini:
# DataSource config
ds = org.apache.shiro.jndi.JndiObjectFactory
ds.requiredType = javax.sql.DataSource
ds.resourceName = jdbc/myds
# JDBC realm config
jdbcRealm = org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.permissionsLookupEnabled = false
jdbcRealm.authenticationQuery = SELECT password FROM user_account WHERE email = ?
jdbcRealm.userRolesQuery = SELECT role_name FROM user_group_membership WHERE email = ?
jdbcRealm.dataSource = $ds
When I deploy the application I get the following Exception (summarised):
FINE: Encountered object reference '$ds'. Looking up object with id 'ds'
FINEST: Applying property [dataSource] value [org.apache.shiro.jndi.JndiObjectFactory#2ca061] on object of type [org.apache.shiro.realm.jdbc.JdbcRealm]
SEVERE: Unable to start Filter: [Unable to set property 'dataSource' with value [$ds] on object of type org.apache.shiro.realm.jdbc.JdbcRealm. If '$ds' is a reference to another (previously defined) object, prefix it with '$' to indicate that the referenced object should be used as the actual value. For example, $$ds].
org.apache.shiro.config.ConfigurationException: Unable to set property 'dataSource' with value [$ds] on object of type org.apache.shiro.realm.jdbc.JdbcRealm. If '$ds' is a reference to another (previously defined) object, prefix it with '$' to indicate that the referenced object should be used as the actual value. For example, $$ds
at org.apache.shiro.config.ReflectionBuilder.applyProperty(ReflectionBuilder.java:373)
at org.apache.shiro.config.ReflectionBuilder.applySingleProperty(ReflectionBuilder.java:198)
at org.apache.shiro.config.ReflectionBuilder.applyProperty(ReflectionBuilder.java:159)
at org.apache.shiro.config.ReflectionBuilder.buildObjects(ReflectionBuilder.java:119)
...
...
Caused by: java.lang.IllegalArgumentException: Cannot invokeorg.apache.shiro.realm.jdbc.JdbcRealm.setDataSource on bean class 'class org.apache.shiro.realm.jdbc.JdbcRealm' - argument type mismatch - had objects of type "org.apache.shiro.jndi.JndiObjectFactory" but expected signature "javax.sql.DataSource"
at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2235)
at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2151)
at org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1957)
...
If instead I define an actual MySQL data source it works fine, e.g.
ds = com.mysql.jdbc.jdbc2.optional.MysqlDataSource
ds.serverName = localhost
ds.user = root
ds.password = root
ds.databaseName = mydb
Any help is much appreciated.
This looks like it is likely a classpath issue. The stack trace references line numbers for ReflectionBuilder that make sense in shiro 1.1.0. However, they don't make sense in shiro 1.2.0. However, since JndiObjectFactory is new in shiro 1.2.0, and both classes are in shiro-core, it seems likely that you have both jars on the classpath.
You should first look in your war file, make sure that both jars aren't in there. If they are, you can fix your war file by removing the 1.1.0 version. If not, I would begin looking to see if you are inheriting an old version of shiro through the glassfish infrastructure somehow.
I'm pretty new to JBoss and Seam. My project has a REST service of the style
#Path("/media")
#Name("mediaService")
public class MediaService {
#GET()
#Path("/test")
public Response getTest() throws Exception {
String result = "this works";
ResponseBuilder builder = Response.ok(result);
return builder.build();
}
}
I can reach this at http://localhost:8080/application/resource/rest/media/test. However, I don't like this URL at all and would prefer something much shorter like http://localhost:8080/application/test.
Can you please point me in the right direction on how to configure the application correctly? (Developing using Eclipse)
web.xml will contain seam resource servlet mapping , this should be modified to /*, and if you have more configuration to the path it will be in components.xml ,if it is resteasy seam is configured to use, it will look like the following
<resteasy:application resource-path-prefix="/rest"/>