Creating gadgets

The creation of a gadget is very simple. This recipe will walk you through the process of creating and deploying a gadget.

Getting ready

You need to create a sample bundle where you will add and deploy your gadget. For example, we will create a Skype Talk client where a user can log into the portal and chat with friends through a customized window. The steps will therefore be:

  • Creating a pom.xml file for a new Maven project
  • Creating a web.xml file , which is mandatory for deploying a gadget project
  • Creating a gadget.xml file where the gadgets are declared
  • Creating the source directories and the code

How to do it...

Go through the following steps to create the new Skype gadget:

  1. Add a very basic pom.xml file in your new Maven project as follows:
    <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/maven-v4_0_0.xsd">
       <modelVersion>4.0.0</modelVersion>
       <groupId>my</groupId>
       <artifactId>skypeGadget</artifactId>
       <version>1.1.0-GA</version>
       <packaging>war</packaging>
       <name>GateIn Portal Skype Gadget Application</name>
    
       <build>
          <finalName>skypeGadget</finalName>
       </build>
    </project>
  2. Add a very basic web.xml file in the src/main/webapp/WEB-INF directory of your project:
    <!DOCTYPE web-app
     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
      "http://java.sun.com/dtd/web-app_2_3.dtd">
    
    <web-app>
      <display-name>skypeGadget</display-name>
    </web-app>
  3. Finally, add the following content in the gadget.xml file of your src/main/webapp/WEB-INF directory:
    <gadgets
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.gatein.org/xml/ns/gatein_objects_1_2 http://www.gatein.org/xml/ns/gadgets_1_0"
        xmlns="http://www.gatein.org/xml/ns/gadgets_1_0">
       
       <gadget name="SkypeTalk">
          <path>/gadgets/SkypeTalk/SkypeTalk.xml</path>
       </gadget>
    </gadgets>

    Note

    In this example, only one Gadget is declared, you can add multiple gadgets in the portlet.xml file and set separated configuration files.

  4. Create the folder for the source.

    If you created a Maven project through Eclipse, we will, by the end of the project, have the following directory/folder structure (see Chapter 1, Getting Started for more details on Maven and Eclipse):

    How to do it...
  5. Now look at the details of the gadgets. Create SkypeTalk.xml in the SkypeTalk folder as declared in the gadget.xml file:
    <?xml version="1.0" encoding="UTF-8" ?>
    <Module>
       <ModulePrefs author="Cookbook Developer"
                    title="Skype Talk"
                    directory_title="Skype Talk"
                    title_url="http://www.vige.it"
                    description="Speak with your friends on Skype."
                    thumbnail="images/SkypeTalk.gif"
                    height="227">
          <Locale lang="ar" language_direction="rtl"/>
       </ModulePrefs>
       <Content type="html">
          <![CDATA[
             ...
          ]]>
       </Content>
    </Module>

    The XML file declares an image to put be in the SkypeTalk/images folder. It will be used in the administration portlets as metadata for the presentation.

    Now you need the JavaScript code. Of course, the more complex the application is, the bigger the JavaScript code will be . Luckily, Skype provides JavaScript to use on their website, so you can t

  6. Connect to: http://www.skype.com/intl/en-us/tell-a-friend/get-askype-button, and the following form will open:
    How to do it...
  7. Put your Skype username and choose a button in the window. You will end up with this code:
    <script type="text/javascript" src="http://download.skype.com/share/skypebuttons/js/skypeCheck.js"></script>
    <a href="skype:yourusername?call"><img src="http://download.skype.com/share/skypebuttons/buttons/call_green_white_153x63.png" style="border: none;" width="153" height="63" alt="Skype Me™!" /></a>
  8. Put all the preceding code in the CDATA tag of SkypeTalk.xml.
  9. Now the project is ready. Create the target war with the Maven command:
    mvn install
    
  10. Deploy it in the application server.
  11. Go to the Application Registry and click on the Import Applications button. The new gadget will be added:
    How to do it...
  12. Go to the dashboard to insert the gadget in the page. Open the Gadget box by clicking on the Add Gadgets link and choose the Skype Talk gadget. This will be the result:
    How to do it...

    You have seen the creation of the gadget through a project. There is, however, an alternative method for creating gadgets. The steps required are as follows:

  13. Go to the Application Registry and click on the Gadget button at the top right:
    How to do it...
  14. Click on Create a new gadget in the second toolbar of the Application Registry. You will see a smart form as shown in the following screenshot:
    How to do it...
  15. Type the title of the gadget in the Name field, for example Skype Talk, and in the Source field, the content of SkypeTalk.xml.
  16. Click on the Save button; the gadget is ready. The gadget, as for the previous example, will be deployed automatically in the JCR repository.

How it works...

When you deploy an application gadget, the Gadget Deployer is called. It is an event listener that catches all applications containing the WEB-INF/gadget.xml file. The deploy process is divided into three phases:

  1. Validation of gadget.xml.
  2. Creation of the importers according to the type of gadget. They can be remote or local.
  3. The gadget is passed to the Gadget Registry Service, which registers the gadget in the registry and executes the importers to register the metadata in the JCR Repository.

The configuration of the deployer can be found in the portal/WEB-INF/conf/portal/application-registry-configuration.xml file:

<component>
  <key>org.exoplatform.application.gadget.GadgetDeployer</key>
  <type>
     org.exoplatform.application.gadget.GadgetDeployer
  </type>
</component>

There's more...

You have seen in this recipe how to create local gadgets. You can also import remote gadgets. The only difference is that the resources will not be registered in the internal repository, but will be called remotely. Here is an example of a remote YouTube gadget declaration through the gadget.xml file:

   <gadget name="RemoteYouTubeGadget">
      <url>http://hosting.gmodules.com/ig/gadgets/file/100080069921643878012/youtube.xml</url>
   </gadget>

The same thing can be done through the web console, by simply clicking on the Add a remote gadget button shown in the preceding screenshot and choosing the gadget XML URL.

Note

Note that, if your gadget is local, many resources of the Google Gadgets are called remotely because the imported code present in the Content field of the gadget module descriptor contains remote calls. Consider rewriting that code if you want to avoid remote calls in your page.

See also

  • Chapter 1, Getting Started
  • The Migrating a portlet that uses JCR in Chapter 8, Migrating from Existing Portals
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.34.80