Play Framework custom java extensions for template - java

I am using Play Framework 1.2.5, and trying to use DateTime from Joda Time instead of the usual java.util.Date. I am trying to implement a format method for use in my views.
The Play documentation says I can create my own custom java extensions for use in templates, but it doesn't seem to be working for me. I have followed the example in the docs to no avail.
My custom extension:
package ext;
import org.joda.time.DateTime;
import play.templates.JavaExtensions;
public class DateTimeExtensions extends JavaExtensions {
public static String format(DateTime datetime, String format) {
return datetime==null ? "" : datetime.toString(format);
}
}
My template code:
${subProject?.startDate?.format('yyyy-MM-dd')}
And the error I am receiving:
Exception raised was MissingMethodException : No signature of method: org.joda.time.DateTime.format() is applicable for argument types: (java.lang.String) values: [yyyy-MM-dd]
It looks like Play isn't detecting my custom extension as the documentation says it should. Does anyone have any suggestions on how to make this work?

Your extension class looks good to me. The documentation states that you have to restart your application for the extension to become active. If that doesn't work, try running play clean. Doing so deletes temporary files, including cached bytecode, which will hopefully resolve your issue.

Related

ebay OAuth 2.0 library Cannot Resolve method initialize()

i wanted to write a bit for Android ebay client.
but im struggeling with the first probleme.
first i start a new Java Android Project with IntelliJ
I want to use this Library ebay-oauth-android-client
like described on Git:
Obtaining Library
This library is distributed via maven central repository. To use this
library, include the below as dependency in your project
dependencies {
compile 'com.ebay.auth:ebay-oauth-android-client:1.0.1'
}
i put this snippet in my Gradle.build and replace compile with implementation since compile is depricated.
so far so good. gradle import this library.
but the next step not working for me:
Application Setup
Before performing OAuth, the library should be initialized with details about your application from eBay developer portal. The library uses
Client ID. For details see Getting your OAuth credentials
Redirect Uri. for details see Getting your Redirect_Uri
Url encoded list of scopes. for details see Specifying OAuth scopes
Use these details in ApiSessionConfiguration.initialize() as shown below:
ApiSessionConfiguration.initialize(
apiEnvironment = ApiEnvironment.PRODUCTION,
apiConfiguration = ApiConfiguration(
<Client ID>,
<Redirect Uri>,
<space separated scopes>
)
)
So i try to call initialze:
my Code with error
But when i try that the Compiler tells me that:
cannot find symbol method initialize(<null>)
When i Jump to the Class Declaration of ApiSessionConfiguration is written that:
// IntelliJ API Decompiler stub source generated from a class file
// Implementation of methods is not available
package com.ebay.api.client.auth.oauth2.model
public final class ApiSessionConfiguration private constructor() {
public companion object {
private final val instance: com.ebay.api.client.auth.oauth2.model.ApiSessionConfiguration /* compiled code */
public final fun getInstance(): com.ebay.api.client.auth.oauth2.model.ApiSessionConfiguration { /* compiled code */ }
public final fun initialize(apiEnvironment: com.ebay.api.client.auth.oauth2.model.ApiEnvironment, apiConfiguration: com.ebay.api.client.auth.oauth2.model.ApiConfiguration): com.ebay.api.client.auth.oauth2.model.ApiSessionConfiguration { /* compiled code */ }
}
public final var apiConfiguration: com.ebay.api.client.auth.oauth2.model.ApiConfiguration? /* compiled code */
public final var apiEnvironment: com.ebay.api.client.auth.oauth2.model.ApiEnvironment? /* compiled code */
}
i dont really understand what im doing wrong. in the sample file on Git ApiSessionConfiguration.initalize() is called without any errors.
i already tried to Invalidate Cache, Clean Build, and start over again.
when i try to import the library from Project Structure Librarys New from Maven repo it says:
no files were downloaded...
Doesn't it can resolve initialize method with single argument?
Did you tried initialize method with two arguments?
Their sample app takes 2 arguments:
https://github.com/eBay/ebay-oauth-android-client/blob/master/sample/src/main/java/com/ebay/api/client/auth/MainActivity.kt#L37-L44
Updated:
But to access to Kotlin companion object function from java you need to call ApiSessionConfiguration.Companion.initialize method

akka with play framework

still learning to master akka java with play framework. I have a code snippet below. It was working fine but has decided to give some headaches.
public class Application extends Controller {
static ActorRef masterActor;
RubineActor rubineactor;
public static Result index() {
return ok(index.render(null));
........ somecode
}
it was working fine but now my eclipse juno complains that it cannot resolve the index object in the return line . I am new to both akka and play framework . Can someone please explain what is happening to me. cos have to submit the project as my final year project. thanks
Your problem is not related to Akka, it's a template concern.
The variable index is provided by a template import, certainly import views.html.*;
Eclipse sometimes cannot resolve this object because it is generated automatically by Play after the first request.
Templates are compiled as standard Scala functions, following a simple naming convention. If you create a views/Application/index.scala.html template file, it will generate a views.html.Application.index class that has a render() method.
See the hello word sample for a concrete exemple.

How to modify/recreate Html.fromHtml()?

I am not that good to understand all the possibility of Java, especially if it's not my code.
So, http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/text/Html.java#Html.fromHtml%28java.lang.String%29 , is it all I need to modify Html.fromHtml() ?
But I don't understand how it's works : Is it a good way to create a new class like Html2 and copy/paste all the code ? When I do that I have some errors that I don't understand :
private static class HtmlParser {
private static final HTMLSchema schema = new HTMLSchema();
}
He tells my Htmlschema cannot be resolved to a type, and to add the class HtmlSchema... but where can i find it ?
And this :
return XmlUtils.convertValueToInt(color, -1);
XmlUtils cannot be resolved.
The rest of the errors have been solved with the help of Eclipse
He tells my Htmlschema cannot be resolved to a type, and to add the class HtmlSchema... but where can i find it ?
import org.ccil.cowan.tagsoup.HTMLSchema;
HTMLSchema is from TagSoup, a JAR used inside of Android's frameworks but not exposed through the Android SDK.
XmlUtils cannot be resolved.
import com.android.internal.util.XmlUtils;
XmlUtils is a class from the Android firmware, not exposed through the Android SDK.

Changing the Functionality of a Java ImageIO class

I was working with
javax.imageio.ImageIO class
The one provided by sun doesn't provide support for reading .tif files. So if I try to read a .tif file, it just returns a null. Then I downloaded this api from oracle's website and included it in the classpath. This api uses jni as was evident from a .so file in that folder. After that I didn't have to change anything in my code and it worked. How could this happen? Wouldn't the class names have clashed?
There were 3 things in the api that i had downloaded:
clibwrapper_jiio.jar
jai_imageio.jar
libclib_jiio.so
I didn't have to do any additional import. In fact, the functionality provided by the ImageIO class was enchanced
I am really curious about how this works.
Here is the class in javax.imageio package. The class has been declared as final. And it does some weird complex stuff that I can't understand. Could someone explain how to achieve this effect with a simpler example.
http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageIO.html
ImageIO has a scanForPlugins(...) method. I'd imagine that on class load time it takes a peek around the CLASSPATH and looks for anything that could extend its functionality.
The javadoc which hints to this is here.
You could do something similar by putting a static block in one of your classes
public class MyClass {
public static scanForExtensions() {
... code looking for extensions goes here ...
... for each found extension, register them in the "ExtensionRegistry"
ExtensionRegistry.register(extension);
}
static {
scanForExtensions();
}
public void doSomething(String input) {
for (Extension extension : ExtensionRegistry.getExtensions()) {
if (extension.canHandle(input)) {
extension.handle(input);
return;
}
}
throw UnhandledInputException("No extension to handle " + input);
}
}
Java's Image IO works using the Service Provider Interface (see links below for more details).
JavaSound works the same way. To add support for (e.g.) MP3 to JavaSound, it is only necessary to add the mp3plugin.jar of the JMF to the run-time class-path, then JavaSound can decode MP3 files as easily as it can a WAV (using the exact same classes - very funky).
I expect the installation of JAI did a similar thing, by adding a bunch of service providers for different image types to the lib directory of the JRE.
ImageWriterSpi
Creating Extensible Applications With the Java Platform.

How to access DLL methods in Java code using JNA?

By running System.loadLibrary("myAPI"), I verified that the DLL file "myAPI.dll" can be successfully loaded into my Eclipse Java project. Now I need to call methods specified inside this DLL file from my Java code. To do this, I added JNA to my Java project. Then I wrote the below-given code snippet that should be able to get instances of classes IProject and ProjectFactory (specified in the DLL file).
I still don't understand how to properly implement this with JNA. I checked different threads, e.g. this one, but the ones I checked don't provide an answer. Any help is highly appreciated. Thanks.
import com.sun.jna.Library;
import com.sun.jna.Native;
public class MyClass {
public interface myAPI extends Library {
//...
}
void LoadProj() {
myAPI api = (myAPI) Native.loadLibrary("myAPI",myAPI.class);
String fileName = "xxx.sp";
IProject project; // this is wrong but shows what I am trying to do
try {
project = ProjectFactory.LoadProject(fileName);
}
catch (Exception ex) {
MessageBox.Show(this, ex.Message, "Load failure");
}
}
}
Not sure what problem you are facing but as a practice your myAPI interface should declare all the methods verbatim with appropriate parameter mapping. I don't see any methods inside your interface.
Please checkout the this link as well as the link mentioned above by #Perception
If there are no Java classes or Java source hidden inside this DLL (which would be ... strange), then it will never work this way. You can't instantiate C# classes or use C# interfaces. MessageBox.Show( isn't Java either, it is Windows Forms code.

Categories

Resources