Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Wednesday, April 15, 2009

DTPtv - Part 1 - Using YouTube APIs in Eclipse

Hey there...

So yes, I'm back to talking about DTPtv... (You can see my introduction to the series here.)

YouTube, LLCImage via Wikipedia

Now that we know generally what we want to do, we'll start by focusing on making the YouTube APIs usable in Eclipse and perform a simple test. Easy enough, right?

So the first thing I had to do was grab the YouTube jars from Google. I found those here. I grabbed the latest version of gdata-samples.java. The YouTube/Google APIs also have some dependencies, so I had to go out and grab imap.jar, mailapi.jar, pop3,jar, and smtp.jar. I was able to re-use a plug-in wrapper for javax.activation.jar from Orbit, which I'll talk about in a sec.

With all of the jars downloaded, I just had to create an Eclipse plug-in wrapper so they were available to other plug-ins. To do this is cake... Right-click in the Package Explorer, select New->Project, and in the list of Wizards select "Plug-in from existing JAR archives". Select your external jars (it will copy them into the plug-in wrapper). I named mine "com.google.gdata.youtube" and unchecked the "Unzip the JAR archives into the project" so it kept the jars as jars, not source code. Click Finish and watch the magic happen.

When it's done, I had to do one last thing to add a dependency. The Google APIs still depend on the javax.activation.jar...

Do you remember I mentioned the Orbit project? Well, you can read more about it here, but the idea is that it provides a repository for a number of third-party projects/resources that are shared across Eclipse. These have already gone through the IP process and are approved for our use. (Note however that even if a library is approved by the Foundation for use by all projects, project teams must still fill out a Contribution Questionnaire and notify the Foundation of their intentions to use a library). And there are quite a few of them. If you look at the latest build (from back in August 2008), you'll see 83 different packages available.

In this case, I just want javax.activation, so I locate it in the list, download it, and drop it in my Eclipse environment. Once the workbench picks it up, I can add it as a dependency in the MANIFEST.MF file for my wrapper plug-in.

Cool. So that pretty much wraps up my jar wrapper plug-in. Not too tough there.

So now what? Now that we have this plug-in, we can write a quick little application to do something with it.

I've gone ahead and created a simple class that does a very basic YouTube search...
package org.eclipse.datatools.sample.utube.sandbox;

import java.io.IOException;
import java.net.URL;

import com.google.gdata.client.youtube.YouTubeQuery;
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.youtube.VideoFeed;
import com.google.gdata.util.ServiceException;

public class UTubeUtils {

public static String TR_FEED_URL = "http://gdata.youtube.com/feeds/api/standardfeeds/top_rated";//$NON-NLS-1$

/**
* Return a list of video entries back to the calling method
*/
public static VideoFeed getResults(String author, String title) throws IOException, ServiceException
{
YouTubeService myService = new YouTubeService(
"", //$NON-NLS-1$
""); //$NON-NLS-1$

String VIDEO_FEED = TR_FEED_URL;
YouTubeQuery query = new YouTubeQuery(new URL(VIDEO_FEED));

//set the author
if( (author != null) && author.length() > 0)
{
query.setAuthor(author);
}
//set the actual query string
if((title != null) && title.length() > 0)
{
query.setFullTextQuery(title);
}

//choose most viewed as the ordering
query.setOrderBy(YouTubeQuery.OrderBy.VIEW_COUNT);

//get the video feed
VideoFeed feed = myService.query(query,VideoFeed.class);
return feed;
}
}

So all that this really does is get a list of the top rated videos currently at YouTube. Pretty straightforward.

You'll notice a couple of things about the code. You have to have a developer key (your own ID as a developer or one for a particular company) and a client ID (seems you can have many of these). This basically lets the Google & YouTube APIs know that you're legitimately asking for data and aren't some rogue hacker trying to cause trouble. You can get these two items here. Once you have them, you can just create new constants for them and just use the constants.

So if you swap your developer key and client ID into the above code, it should do a quick search. But how should we test it?

I like creating new plug-in projects with example menu actions. To do this is pretty easy... Right-click in the Package Explorer, select New->Project, and in the list of Wizards select "Plug-in Project". Name it and set the plug-in ID and other info, and on the "Templates" page in the wizard, select "Plug-in with a popup menu." By default it keys off an IFile, so you can get to it from the Navigator or Project Navigator in the workbench.

It goes off and creates the basic code and you can then use your new utility class pretty easily by changing the run() method in the action class to look something like this:

    public void run(IAction action) {
try {
VideoFeed feed = UTubeUtils.getResults(null, null);
if (feed != null) {
if (feed.getEntries() != null && feed.getEntries().size() > 0) {
System.out.println("Found some videos...");
Iterator<VideoEntry> iter = feed.getEntries().listIterator();
while (iter.hasNext()) {
VideoEntry entry = iter.next();
System.out.println("Video Entry: " + entry.getTitle().getPlainText());
}
}
// clean up
feed = null;
}
} catch (ServiceException se) {
se.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
}
}

When you run the workbench and right-click on your action in the Navigator, you should see something like this in your development workbench console view...

Success! We have a plug-in wrapper for our YouTube jars and their dependencies. And we've verified that we can use those APIs in an Eclipse environment.

Next we have to figure out how to hook up YouTube to DTP and show a video in the workbench. And after that we can fine tune our look and feel to make it easier for our users.

Maybe this was a bit longer to write up than I'd thought originally, but this part of the process only took me about half a day when I was creating this code the first time.

Questions? Comments? Drop me a note here and I'll be happy to get back to you.

Next time I'll write about hooking up YouTube and DTP and how I used the built-in Eclipse web browser UI component to show videos.

--Fitz


Reblog this post [with Zemanta]

Monday, April 13, 2009

DTP APis and How to Use a Transient Connection Profile

Hi there...

On the DTP newsgroup we had a question about using DTP APIs to create a new transient connection profile and then use that to execute some DDL...

It's pretty easy actually... The trick for the transient profile is knowing all the bits and pieces you have to have ahead of time, like the:
  • provider ID, which is the connection profile type ID
  • vendor and version, which relate to the vendor/version of the database you're connecting to
  • and then the driver path. Note that you can also use a pre-defined driver and get the DriverInstance from the DriverManager, then retrieve various properties like the vendor, version, class name, and driver path from there
So you end up with something like this:
    private static String providerID = "org.eclipse.datatools.connectivity.db.derby.embedded.connectionProfile"; //$NON-NLS-1$
private static String vendor = "Derby"; //$NON-NLS-1$
private static String version = "10.1"; //$NON-NLS-1$

private static String jarList = "C:\\Derby10.1.3.1\\db-derby-10.1.3.1-bin\\lib\\derby.jar"; //$NON-NLS-1$
private static String dbPath = "c:\\DerbyDatabases\\MyDB"; //$NON-NLS-1$
private static String userName = ""; //$NON-NLS-1$
private static String password = ""; //$NON-NLS-1$

private static String driverClass = "org.apache.derby.jdbc.EmbeddedDriver"; //$NON-NLS-1$
private static String driverURL = "jdbc:derby:" + dbPath + ";create=true"; //$NON-NLS-1$ //$NON-NLS-2$

public static Properties generateTransientDerbyProperties() {
Properties baseProperties = new Properties();
baseProperties.setProperty( IDriverMgmtConstants.PROP_DEFN_JARLIST, jarList );
baseProperties.setProperty(IJDBCConnectionProfileConstants.DRIVER_CLASS_PROP_ID, driverClass);
baseProperties.setProperty(IJDBCConnectionProfileConstants.URL_PROP_ID, driverURL);
baseProperties.setProperty(IJDBCConnectionProfileConstants.USERNAME_PROP_ID, userName);
baseProperties.setProperty(IJDBCConnectionProfileConstants.PASSWORD_PROP_ID, password);
baseProperties.setProperty(IJDBCConnectionProfileConstants.DATABASE_VENDOR_PROP_ID, vendor);
baseProperties.setProperty(IJDBCConnectionProfileConstants.DATABASE_VERSION_PROP_ID, version);
baseProperties.setProperty( IJDBCConnectionProfileConstants.SAVE_PASSWORD_PROP_ID, String.valueOf( true ) );
return baseProperties;
}

public void createTransientDerbyProfile() throws Exception {
ProfileManager pm = ProfileManager.getInstance();

IConnectionProfile transientDerby = pm.createTransientProfile(providerID, generateTransientDerbyProperties());
// do something with the profile

}


And then once you have your transient profile, connect, get the Java connection object, and execute your DDL...
        IStatus status = transientDerby.connect();
if (status.equals(IStatus.OK)) {
// success
java.sql.Connection conn = getJavaConnectionForProfile(transientDerby);
if (conn != null) {
try {
java.sql.Statement stmt = conn.createStatement();
java.sql.ResultSet results = stmt.executeQuery("<INSERT QUERY/DDL HERE>");
} catch (java.sql.SQLException sqle) {
sqle.printStackTrace();
}

}

} else {
// failure :(
if (status.getException() != null) {
status.getException().printStackTrace();
}
}


So not too bad. Great question though! Hope this helps!

--Fitz
Reblog this post [with Zemanta]

Thursday, March 19, 2009

DTP at EclipseCon 2009

Hey there!

Yes, it's almost that time again... EclipseCon 2009 starts with a bang on Monday. Can you believe it? It's already here!!

I head out to the (hopefully sunny) state of California on Sunday morning early for a boatload of meetings that afternoon and then DTP has a tutorial bright and early Monday morning at 8am.

But I thought I'd fill everyone in on what's cool in the world of DTP at EclipseCon this year...

We have our tutorial obviously -- "Using and Extending Eclipse Data Tools (DTP)" on Monday morning at 8am. If you're planning on attending, please check out the list of pre-requisites. We'll start with the DTP tooling, talk about DTP APIs, and then go a bit into how to extend the ODA and SQL editor for your own particular uses. Linda Chan (Actuate), Brian Payton (IBM), and myself will be presenting.

We also have a couple of long talks...
And a number of short talks that will be collected into one curated session hosted by yours truly...
So it's not forgotten, we also have a Birds of a Feather session on Monday night. If you want to come and chat, ask questions, and see what's going on in the world of DTP please drop by!
And if you're going to attend the Eclipse Community Spotlight panel at the end of the conference, you'll get to see a bunch of us talking about what's going on in the world of Eclipse.

So be sure to check us out while you're at EclipseCon! I hope to see you there!

--Fitz
Reblog this post [with Zemanta]

Monday, January 19, 2009

Cool DTP Talks at EclipseCon 2009

Hey all!

I'm always amazed by the depth and breadth of talks at EclipseCon and this year is no different. In the Data Tooling category, we have a diverse set of talks (you can see the list here) on everything from some new tooling we've been working on, updates to the Graphical SQL Query Builder, how Ingres is rolling DTP components in new and unique ways, to using YouTube in DTP and how a commercial vendor (IBM) is using and extending DTP APIs for their PureQuery product.

I have to admit I'm sort of partial to the YouTube presentation I'm doing :), but I'm very curious to hear Ingres and IBM talk about their tooling and products and how DTP is playing a role in those.

For those of you just getting started with DTP, we have a tutorial scheduled for the Monday of the conference that's going to walk through a ton of topics from adding a new JDBC driver to the mix, to supporting a new database, and customizing SQL syntax and so on...

DTP is much more than just a great set of tools for data access... It's a great community. And EclipseCon is when that community comes together.

Come join our community!

--Fitz
Reblog this post [with Zemanta]

Wednesday, August 20, 2008

Creating an Actual SQLite Connection Profile (minus the UI)

Hi there!

So now we have the majority of our work done. We have a driver-wrapper plug-in, a driver definition, and an overridden catalog loader. What's next? Wrapping the functionality in a nice, easy to use connection profile!

Note: Previous articles in this series cover the following topics: Catalog Loaders, Driver Templates, and the Driver Framework.

Many moons ago, we talked at a high level about the driver template & driver definition frameworks. It's now time to talk briefly about the connection profile framework.

It all boils down to this... A connection profile manages a connection to something. Right now in DTP we connect to JDBC databases and file systems for the most part. But the Sybase WorkSpace product also uses DTP to connect to application servers, LDAP, UDDI repositories, and much more. So it's not limited in any way.

With that in mind, a JDBC database connection profile, such as the one we want to create for SQLite, just needs to manage a JDBC connection under the covers. We'll add a layer on top of that to attach the SQL Model to the connection so we can display the database specifics in the Data Source Explorer tree.

In the DTP Ganymede (1.6) release, we've really simplified creating a new connection profile if it's associated with a db definition vendor/version and a driver template. So we'll take advantage of that for SQLite.

To create a connection profile, we will go to the org.eclipse.datatools.enablement.sqlite plug-in project and create a couple of classes and two extension points. These steps are kind of chicken & egg - the order isn't really important so long as you get them all done.

Step 1: Create a new connection factory and connection class for SQLite. These are the actual raw connections that our SQLite connection profile will manage for us.

The connection class is pretty easy. We're just going to extend the Generic JDBC JDBCConnection class for SQLite so we have our own specialized version of it.

That code looks like this:
package org.eclipse.datatools.enablement.sqlite.connection;

import org.eclipse.datatools.connectivity.IConnectionProfile;
import org.eclipse.datatools.connectivity.db.generic.JDBCConnection;

public class SQLITEJDBCConnection extends JDBCConnection {

/**
* @param profile
* @param factoryClass
*/
public SQLITEJDBCConnection(IConnectionProfile profile,
Class factoryClass) {
super(profile, factoryClass);
}
}

The connection factory requires a little more work, but not much more:
package org.eclipse.datatools.enablement.sqlite.connection;

import org.eclipse.datatools.connectivity.IConnection;
import org.eclipse.datatools.connectivity.IConnectionProfile;
import org.eclipse.datatools.connectivity.db.generic.JDBCConnectionFactory;

public class SQLITEJDBCConnectionFactory extends JDBCConnectionFactory {

public SQLITEJDBCConnectionFactory() {
super();
}

public IConnection createConnection(IConnectionProfile profile) {
SQLITEJDBCConnection connection = new SQLITEJDBCConnection(profile, getClass());
connection.open();
return connection;
}
}

Basically in the connection factory, we're just creating one of our new SQLiteJDBCConnection class instances for the profile that's passed in.

Step 2: We want to add a new extension point to the plugin.xml in the org.eclipse.datatools.enablement.sqlite plug-in project: org.eclipse.datatools.connectivity.connectionProfile.
This extension point has a couple of nodes we're going to create beneath it: connectionFactory and connectionProfile.

Let's define our connectionProfile first, so we have the connection profile ID to use for the connectionFactory.



You can see from the screen that we're giving our connection profile the following properties:
  • id = org.eclipse.datatools.enablement.sqlite.connectionProfile
  • category = org.eclipse.datatools.connectivity.db.category (this ensures that our connection profile shows up under the "Databases" category in the DSE)
  • name = SQLite Connection Profile
  • icon = icons/jdbc_16.gif (you can copy this from the Generic JDBC connection profile plug-in)
  • pingFactory = org.eclipse.datatools.enablement.sqlite.connection.SQLITEJDBCConnectionFactory (our new connection factory class we created in step 1)
Then we define our connectionFactory:



Our connectionFactory extension has the following properties:
  • id = java.sql.Connection (this maps to the type of connection this connection factory/connection class maps back to -- in this case, a JDBC connection)
  • class = org.eclipse.datatools.enablement.sqlite.connection.SQLITEJDBCConnectionFactory (our connection factory class)
  • profile = org.eclipse.datatools.enablement.sqlite.connectionProfile (our SQLite connection profile ID from the connectionProfile extension)
  • name = SQLite Connection Factory
So now we have a connection profile for SQLite in DTP. Now all we need is a user interface (wizard, wizard page, property page, and driver UI) and we'll be golden!

That's what we'll cover next time.
--Fitz

Reblog this post [with Zemanta]