Menu

How to create session from resourceResolver object?

Sometimes we need a session to work with some special JCR API which we can,t use with the help of resourceResolver then we can create an instance of a session in the same class file using the object of resourceResolver. Below is the code snippet.

First, we need to inject the ResourceResolverFactory interface to create a new resourceResolver in AEM. You could inject using the SCR annotation @Reference or Sling Model annotation @Inject.
@Reference
private ResourceResolverFactory resolverFactory;
Now create the ResourceResolver object using resourceResolverFactory. This line of code will create a rsourceResolver with administrator login.
ResourceResolver resourceResolver=resolverFactory.getServiceResourceResolver(null);
Now will create the instance of Session using the object of resourceResolver.
Session session = resourceResolver.adaptTo(Session.class);

How to run a Java Application or program without main method?

Below is the code snippet to execute and run a java program without main method.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
package rashid.jorvee;

import javafx.stage.Stage;

public class RunAJavaApplicationWithoutMainMethod extends javafx.application.Application {
 
 @Override
 public void start(Stage primaryStage) throws Exception {
  System.out.println("Running Application!!!");
 }
}

Generate i18n key node element for AEM

Create an excel file with i18n key and value. Then in the next cell apply the concatenate formula to create the i18 XML element from given key and value. Below is the concatenate formula.
=CONCATENATE("<",A2," jcr:primaryType=","""","sling:MessageEntry",""""," sling:key=","""",A2,""""," sling:message=","""",B2,"""","/>")
After applying the formula you will get the xml element as below. Now just copy and paste these elements in your i18n language.xml file.

<userName jcr:primaryType="sling:MessageEntry" sling:key="userName" sling:message="Rashid"/>