DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

The Latest Coding Topics

article thumbnail
Concurrent Programming in Groovy
It seems that the Groovy has a project for just about anything. That's one of the reasons why the language is so popular. The GPars library is an especially useful project in this new era of mult-core processors and concurrent programming. Formerly known as GParallelizer, GPars offers a framework for handling tasks concurrently and asynchronously while safe-guarding mutable values. DZone recently got an update on the project's latest news from project lead Václav Pech, and we've provided some examples of GPars concepts. GPars uses some of the best concepts from emerging languages and implements them for Groovy. Its actor support was inspired by the Actors library in Scala and the SafeVariable class in GPars was inspired by Agents in Clojure. The Groovy-based APIs in GPars are used to declare which parts of the code should be run concurrently. Objects can be enhanced with asynchronous methods to perform collections-based operations in parallel based on the fork/join model. GPars also has a Dataflow concurrency model that offers an alternative model that is inherently safe and robust due to algorithms that prevent having to deal with live-locks and race-conditions. The SafeVariable class is another technology in GPars that alleviates problems with concurrency by providing a non-blocking mt-safe reference to mutable state when Java libraries are integrated. Finally the Parallelizer, Asynchronizer, and Actors are some of the most interesting concepts in GPars. Actors Actors can be generated quickly to consume and send messages between each other even across distributed machines. You can build a messaging-based concurrency model with actors that are not limited by the number of threads. What was once only available to Scala developers, GPars now brings to Java and Groovy developers. Actors perform three different operations - send messages, receive messages and create new actors. New actors are created with the actor() method passing in the actor's body as a closure parameter. Inside the actor's body loop() is used to iterate, react() to receive messages, and reply() to send a message to the actor, which has sent the currently processed message. Here is how to create an actor that prints out all messages that it receives: import static groovyx.gpars.actor.Actors.* def console = actor { loop { react { println it } } } The loop() method ensures that the actor doesn't stop after processing the first message. Messages are sent using the send() method or the << operator. Here is an example of the sendAndWait () method in a message: actor << 'Message' actor.send 'Message' def reply1 = actor.sendAndWait('Message') def reply2 = actor.sendAndWait(10, TimeUnit.SECONDS, 'Message') def reply3 = actor.sendAndWait(10.seconds, 'Message') The sendAndWait() family blocks the caller until a reply from the actor becomes available. The reply is returned from sendAndWait() as a return value. For non-blocking message retrieval, calling the react() method, with or without a timeout parameter, from within the actor's code will consume the next message from the actor's inbox: println 'Waiting for a gift' react {gift -> if (myWife.likes gift) reply 'Thank you!' } Here is a more 'real world' example of an event-driven actor that receives two numeric messages, generates a sum, and sends the result to the console actor: import static groovyx.gpars.actor.Actors.* //not necessary, just showing that a single-threaded pool can still handle multiple actors defaultPooledActorGroup.resize 1 final def console = actor { loop { react { println 'Result: ' + it } } } final def calculator = actor { react {a -> react {b -> console.send(a + b) } } } calculator.send 2 calculator.send 3 calculator.join() Since Actors can share a relatively small thread pool, they bypass the threading limitations of the JVM and don't require excessive system resources even if an application consists of thousands of actors. There are some more sophisticated actor examples on the old GParallelizer wiki and there's also a nice article on the key concepts behind actors in erlang and scala. The documentation on GPars Actors can be found here. Asynchronizer A major feature of GPars is the Asynchronizer class, which runs tasks asynchronously in the background. It enables a Java Executor Service-based DSL on collections and closures. Inside the Asynchronizer.doParallel() blocks, asynchronous methods can be added to the closures. async() creates a variant of the supplied closure returning a future for the potential return value when invoked. callAsync() calls a closure in a separate thread supplying the given arguments and also returns a future for the potential value. Here is one example of Asynchronizer use: Asynchronizer.doParallel() { Closure longLastingCalculation = {calculate()} Closure fastCalculation = longLastingCalculation.async() //create a new closure, which starts the original closure on a thread pool Future result=fastCalculation() //returns almost immediately //do stuff while calculation performs … println result.get() } Parallelizer Finally, there's the Parallelizer, which is a concurrent collection processor. The common pattern to process collections takes elements sequentially, one at a time. This algorithm however, won't work well on multi-core hardware. The min() function on a dual-core chip can only leverage 50% of the computing power - 25% for a quad-core. Instead, GPars uses a tree-like structure for parallel processing. The Parallelizer class enables a ParallelArray(from JSR-166y)-based DSL on collections. Here is a use exapmle: doParallel { def selfPortraits = images.findAllParallel{it.contains me}.collectParallel {it.resize()} //a map-reduce functional style def smallestSelfPortrait = images.parallel.filter{it.contains me}.map{it.resize()}.min{it.sizeInMB} } Václav Pech told DZone that GPars currently has two new people joining the project - Jon Kerridge and Kevin Chalmers. The two developers are bringing their JCSP Groovy library with them into GPars. Pech said, "Apart from experimenting with the CSP concept, we will also enhance actor remoting and polish a couple of rough edges on the APIs. The documentation and especially the samples also deserve more attention." GPars' documentation is already quite robust. Pech says there quite a few issues queued up in their JIRA, but the previously listed issues remain the top priorities. Depending on the amount of time required to make progress with CSP, the GPars 1.0 release might happen in the summer says Pech. GPars is also developed by Alex Tkachman, a leading developer on the Groovy++ project. In an with Andres Almiray, Tkachman said that some of the work that comes out of Groovy++ might be assimilated into GPars, but no plans are in place yet since Groovy++ developers are still experimenting.
February 22, 2010
by Mitch Pronschinske
· 53,810 Views · 2 Likes
article thumbnail
Top 10 Interesting NetBeans IDE Java Shortcuts I Never Knew Existed
I'm working on updating the NetBeans IDE Keyboard Shortcut Card (which you can always find under the "Help" menu in the IDE) and have learned about a lot of shortcuts I never knew about before. Here's a grab bag of things I have added to the shortcut card (some new, some old that hadn't been included before) that you might find interesting too. Type "fcom" (without the quotes, same as all the rest below) and then press Tab. You now have this, i.e., a brand new code fold: // // Type "bcom" and then press Tab to create the start of a new set of comments in your code: /**/ Type "runn" and press Tab and you'll have all of this: Runnable runnable = new Runnable() { public void run() { } }; If I have this: String something = ""; ...and then below that type "forst" and press Tab, I now have this: String something = ""; for (StringTokenizer stringTokenizer = new StringTokenizer(something); stringTokenizer.hasMoreTokens();) { String token = stringTokenizer.nextToken(); } Also, experiment with "forc", "fore", "fori", "forl", and "forv"! I always knew that "sout" turns into "System.out.println("");" but did you know that (again assuming you first have a string something like above) if you type "soutv" you end up with this: System.out.println("something = " + something); Thanks Tom Wheeler for this tip. Next, here are the new shortcuts that are new from NetBeans IDE 6.9 onwards: as - assert=true; su - super db - double sh - short na - native tr - transient vo - volatile I knew that "ifelse" would resolve to an if/else block. But did you know that if you don't need an 'else', you can simply type "iff", press Tab, and then end up with this: if (exp) { } From NetBeans IDE 6.9 onwards, the "sw" shortcut expands to the following: switch (var) { case val: break; default: throw new AssertionError(); } If you're using while loops, experiment with "whileit", "whilen", and "whilexp". Always remember these: "im" expands to "implements; "ex" to "extends". Other tips along these lines are more than welcome here on NetBeans Zone!
February 19, 2010
by Geertjan Wielenga
· 98,162 Views · 4 Likes
article thumbnail
Factory Method Pattern Tutorial with Java Examples
Learn the Factory Method Design Pattern with easy Java source code examples as James Sugrue continues his design patterns tutorial series, Design Patterns Uncovered
February 18, 2010
by James Sugrue
· 186,328 Views · 8 Likes
article thumbnail
Sending Files With Sinatra
How to send files (attachments) with Sinatra 0.9.4. get '/some/:file' do |file| file = File.join('/some/path', file) send_file(file, :disposition => 'attachment', :filename => File.basename(file)) end I found the documentation was misleading in some cases and plain wrong in others. The key lie is the method "signature": def send_file(path, opts={}) This is confusing because Sinatra only uses options if you specify the disposition to either "inline" or "attachment". Otherwise, it simply throws the File (which doesn't know/care about your options). It is not enough to simply call send_file('/my/file') Although the documentation says File.basename will be uses as the default name, that simply isn't true. Using Firefox, I got "download" as the filename.
February 17, 2010
by Snippets Manager
· 4,596 Views
article thumbnail
Screen Record & Play Using Java
This tip shows how to create a custom movie maker using the Java Media Framework. First the sample code below shows how to capture your screen and creates a nice .jpeg or .gif image of your screen content. import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class ScreenCapture { public static void main(String[] args) throws Exception { Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); Robot rt = new Robot(); BufferedImage img = rt.createScreenCapture(new Rectangle((int) screen .getWidth(), (int) screen.getHeight())); ImageIO.write(img, "jpeg", new File(System.currentTimeMillis() + ".jpeg")); } } The Java Media Framework provides support to put all these images together to make a movie of your screen shot. Download the full custom movie maker code I have put together :). The ScreenRecorder code: package com.easycapture.recorder; import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import java.net.MalformedURLException; import java.util.Scanner; import java.util.Vector; import javax.imageio.ImageIO; import javax.media.MediaLocator; /** * Main class that starts the Recording process of EasyCapture. * * @author Senthil Balakrishnan */ public class Recorder { /** * Screen Width. */ public static int screenWidth = (int) Toolkit.getDefaultToolkit() .getScreenSize().getWidth(); /** * Screen Height. */ public static int screenHeight = (int) Toolkit.getDefaultToolkit() .getScreenSize().getHeight(); /** * Interval between which the image needs to be captured. */ public static int captureInterval = 50; /** * Temporary folder to store the screenshot. */ public static String store = "tmp"; /** * Status of the recorder. */ public static boolean record = false; /** * */ public static void startRecord() { Thread recordThread = new Thread() { @Override public void run() { Robot rt; int cnt = 0; try { rt = new Robot(); while (cnt == 0 || record) { BufferedImage img = rt .createScreenCapture(new Rectangle(screenWidth, screenHeight)); ImageIO.write(img, "jpeg", new File("./"+store+"/" + System.currentTimeMillis() + ".jpeg")); if (cnt == 0) { record = true; cnt = 1; } // System.out.println(record); Thread.sleep(captureInterval); } } catch (Exception e) { e.printStackTrace(); } } }; recordThread.start(); } /** * @throws MalformedURLException * */ public static void makeVideo(String movFile) throws MalformedURLException { System.out .println("#### Easy Capture making video, please wait!!! ####"); JpegImagesToMovie imageToMovie = new JpegImagesToMovie(); Vector imgLst = new Vector(); File f = new File(store); File[] fileLst = f.listFiles(); for (int i = 0; i < fileLst.length; i++) { imgLst.add(fileLst[i].getAbsolutePath()); } // Generate the output media locators. MediaLocator oml; if ((oml = imageToMovie.createMediaLocator(movFile)) == null) { System.err.println("Cannot build media locator from: " + movFile); System.exit(0); } imageToMovie.doIt(screenWidth, screenHeight, (1000 / captureInterval), imgLst, oml); } /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { System.out.println("######### Starting Easy Capture Recorder #######"); Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); System.out.println("Your Screen [Width,Height]:" + "[" + screen.getWidth() + "," + screen.getHeight() + "]"); Scanner sc = new Scanner(System.in); System.out.println("Rate 20 Frames/Per Sec."); System.out .print("Do you wanna change the screen capture area (y/n) ? "); if (sc.next().equalsIgnoreCase("y")) { System.out.print("Enter the width:"); screenWidth = sc.nextInt(); System.out.print("Enter the Height:"); screenHeight = sc.nextInt(); System.out.println("Your Screen [Width,Height]:" + "[" + screen.getWidth() + "," + screen.getHeight() + "]"); } System.out .print("Now move to the screen you want to record"); for(int i=0;i<5;i++){ System.out.print("."); Thread.sleep(1000); } File f = new File(store); if(!f.exists()){ f.mkdir(); } startRecord(); System.out .println("\nEasy Capture is recording now!!!!!!!"); System.out.println("Press e to exit:"); String exit = sc.next(); while (exit == null || "".equals(exit) || !"e".equalsIgnoreCase(exit)) { System.out.println("\nPress e to exit:"); exit = sc.next(); } record = false; System.out.println("Easy Capture has stopped."); makeVideo(System.currentTimeMillis()+".mov"); } } And in order to make the movie, you will need the JpegImagesToMovie class.
February 17, 2010
by Senthil Balakrishnan
· 51,342 Views · 2 Likes
article thumbnail
Interview: Intelligence Gathering Software on the NetBeans Platform
Chris Bohme is the chief software architect at Pinkmatter Solutions – a small, specialized software development company in South Africa. Pinkmatter has been working with a company called Paterva for the past few years to build Maltego - a tool for data visualization, reconnaissance and intelligence gathering. Maltego is used by law enforcement and intelligence agencies, network security professionals and large corporates to discover and analyze information. In a nutshell, how does Maltego work? Maltego models information as entities (e.g., persons, e-mail addresses) and relationships between them. Relationships are discovered by running pluggable functions (called transforms) on the entities. For example, when running a social network transform on my e-mail address, one would discover my Facebook and LinkedIn profiles. Out of the box, Maltego ships with over 150 transforms that mainly relate to open source intelligence. However, an organization using Maltego user can easily create their own transforms that run on their internal data. The concept of transforms makes data gathering very quick and easy which is one of the aspects that sets it apart from some of its competitors like Analyst Notebook, which has been the de-facto tool for investigation and intelligence analysis. Why and how did you choose to use the NetBeans Platform as the basis of this application? We have actually been using the NetBeans Platform at Pinkmatter since 2002, back in the days of NetBeans 3.2, when the NetBeans Platform was not really separate from the IDE and the only real documentation for NetBeans Platform users was the source code. Back then Pinkmatter was building a network security management tool we called “Palantir”, which was never released but which would later form the basis framework for Maltego. (Ironically one of Maltego’s competitors is now made by a company called Palantir Tech.) I was using Forte (Sun’s customized version of NetBeans) as my IDE for Java development and realized that I would need very similar features in Palantir – global selection management, runtime composition (i.e., modules), copy/paste/undo/redo, auto-update, property grid, window manager, system palette etc. So I began reading through the sources and building Palantir as a NetBeans module while trying to remove as much of the IDE parts as possible. I immediately fell in love with its design and complexity (yes, complexity – no matter how long you have been using the NetBeans Platform, there is something new you can learn every day) – but there was a definite beauty to it and I knew that following its architecture guidelines would save me from the certain “spaghetti-death” to which all large UI applications I had seen thus far were doomed from the start. What are the main advantages of the NetBeans Platform to you? On a personal level, working with the NetBeans Platform early on in my developer career has shaped my mindset around application design. As such, the NetBeans Platform source code was one of my most influential teachers when it comes to API design and architecture of large complex applications. I started looking for similar patterns in the frameworks I was building using other programming languages and it has helped me identify designs that are “right” and those that are “wrong”. (When it comes to API design I believe that “truth, like beauty, is not a matter of opinion” :-) ) On the level of Maltego, I think the benefits are fairly obvious – there is a platform that comes with lots “free stuff” right out of the box. And hey, the best thing is, someone else improves, fixes and supports all this free stuff while you can focus on your specific problem domain. If I were to rephrase the question to read “what in the NetBeans Platform couldn’t I live without?” – well, it would be the features related to runtime composition. The fact that components can be registered declaratively (for example in layer files) and are added as modules that get loaded at runtime shapes the overall design and maintainability and is something a modern application cannot do without. As Maltego matures, instead of removing the dependency on some NetBeans APIs and replacing them with our own, we tend to use more and more of what the NetBeans Platform (and even the IDE) has to offer. This is a very good indication to me that a) NetBeans Platform was the right choice to build Maltego on and b) that the evolution of the NetBeans Platform is in line with the needs of its users (well, at least for us). Continue to part 2 of this interview... Were there things that pleasantly surprised you while working with the NetBeans Platform? There were many.... but let’s start with backward compatibility. A lot of the Palantir code from 2002 can still run in NetBeans 6 – that is 3 major versions and 8 years later! – not a small feat to achieve for an API designer. As another example, for the upcoming 3.0 of Maltego we redesigned our underlying information model to allow a user to model entities with a multitude of properties. We needed to allow the user to configure these using many kinds of weird and wonderful type editors... and actually the good old PropertySheet works well for that, can be highly customized and takes up very little screen real estate. In general I am amazed every time how efficiently NetBeans can handle so many modules (and merged layer files)! What could be improved? Well, I have this gripe with the wizard framework. Although sufficient for the IDE, there is a lot to be desired from wizards when used in other applications. How about re-using wizard panels for editing something in a dialog (panels as tabbed panes for example)? Or quick and dirty mechanisms to disable the Cancel button or intercept it to cancel a background thread? (I know, I know, stop complaining, Chris, and contribute something of that sort – yes... one day when Maltego has grown up and I am no longer working nights.) But in the end I think that in spite of all the great efforts that have been made, documentation is still a limiting factor when it comes to the adoption and effective use of the NetBeans Platform. There are a number of really good books, blogs and tutorials, however, I feel there is a need for something like “An Architect’s Guide for Designing Applications for the NetBeans Platform” – something that focuses more on core design decisions that have to be made before getting started. For example, “how is your global selection management to work?” and “what mechanisms does the NetBeans Platform provide for that?” Any tips or tricks for other NetBeans Platform developers? Read every book that has ever been published about the NetBeans Platform. Read and take note of tips published on blogs – you might not need them today but in 6 months time you will remember that there is a smart way to do something. I check planetnetbeans.org every day for interesting articles. Keep a copy of the NetBeans Platform sources around (you can download them in a handy ZIP file and don’t even have to do a checkout). Whenever there is something that you don’t understand or that seemingly does not work, grep the sources for the relevant classes. Don’t feel you have to make use of NetBeans APIs all the time. Sometimes it makes sense to just use a JTable instead of creating a Node implementation with OutlineView. As that component gets more full featured, you can always refactor it and replace it with a suitable View. The default lookup is your friend! Finish this sentence: "If I had known..." Actually, if I had known that it is possible (and easy) to replace the default implementation of ContextGlobalProvider I would have more hair left on my head! (Before I read Tim’s blog entry, activating a TopComponent would amount to changing the global selection – something that is not valid for all applications – and boy did I struggle...) What's the future of the application? We are close to releasing Maltego 3.0 – the next big milestone in the life of our beloved baby. This release brings many new features with it, not least of all a slick new look (thanks to some of the beautiful work done by the likes of Gunnar Reinseth, Mikael Tollefsen and Kirill Grouchnikov): Our ultimate vision is to evolve Maltego into an autonomous information monitoring system – something like an IDS (intrusion detection system), but for information. The threats to organizations (or governments) on the internet are no longer constrained to attacks on their network infrastructure (the origin of the term IDS) but information about them, their competitors or employees floating around on the internet can seriously harm them. Think of it as a highly customizable, intelligent Google Alert, which is fed from the internet as well as private, internal databases. Subsequent releases will bring us closer to that vision with geo-spatial data, time base analyses and live, real time data feeds.
February 15, 2010
by Geertjan Wielenga
· 39,093 Views
article thumbnail
Checkout Multiple Projects Automatically Into Your Eclipse Workspace With Team Project Sets
When working in Eclipse, you’ll often end up with a number of projects in your workspace that constitute an application. You could have a multi-tiered system with a web, server and database project and other miscellaneous ones. Or if you’re an Eclipse RCP developer, you could end up with dozens of plugins each represented by a project. Although multiple projects give you modularity (which is good), they can make it difficult to manage the workspace (which is bad). Developers have to check out each project individually from different locations in the repository. Sometimes they even have to get projects from multiple repositories. This is a painstakingly long and error-prone task. But an easier way to manage multiple projects is with Eclipse’s Team Project Sets (TPS). Creating a workspace becomes as easy as importing an XML file and waiting for Eclipse to do its job. Yes, there are other more sophisticated tools out there that do this and more (eg. Maven and Buckminster) but team project sets are a good enough start if you haven’t got anything set up and may be good enough for the longer term as well, depending on how your team works. Create a Team Project Set to share with other developers It’s easy to create a team project set (TPS). The first thing is to start with a workspace that already has all the projects checked out. Then it’s as easy as choosing File > Export > Team > Team Project Set, selecting the projects you want to export and then entering a file name. Done. But it’s always better to see it in action. In the video, I export 3 projects that I’ve already checked out from Subversion into a TPS file. Notes: You can select which projects should go into the TPS. This way you can exclude irrelevant or personal projects you’ve got in your workspace. Eclipse adds the extension .psf if you don’t provide one. The exported file is an XML file, with the default extension of psf, so in the video the file would be music.psf. There is a project entry for each project you exported that includes the project’s name and its repository location, separated by commas. Once created, the file is easy to edit so go ahead and make your own changes if you want to. Here is an example of what it looks like: svn/repo/music-application/trunk,music-application"/> svn/repo/music-db/trunk,music-db"/> svn/repo/music-web/trunk,music-web"/> Import the Team Project Set to checkout multiple projects into your workspace Now for the fun part. To import a team project set (TPS), start with any workspace (normally an empty one) and choose File > Import > Team > Team Project Set. Choose the TPS file that someone else kindly exported for you and then wait for Eclipse to do its magic. Notes: If you have an existing project in your workspace whose name matches a project in the TPS, Eclipse will prompt you whether you want to overwrite the project. I always choose No To All, since overwriting the project will mean you lose any changes you made to it. But if you have the urge to start from scratch then you can choose Yes. The import also creates a link to the repository in SVN Repositories, so you don’t have to do that. If one already exists, it will not duplicate it but reuse the existing connection. The process may take a while depending on the number of projects in the TPS and the speed of your repo checkouts. You can choose to run the import in the background (as I did in the video), giving you the opportunity to use Eclipse while the import happens. Otherwise, grab some coffee and wait for it to finish the checkouts. Gotcha: You may find that Eclipse 3.4 and lower may actually create a repository connection per project if the repository didn’t exist beforehand, which is not ideal. To solve this, create an initial repository root that’s shared by the projects and then do the import of the TPS. This problem has been fixed in 3.5 Managing the team project set and working with branches I’d recommend checking in the team project set into your repository and versioning/tagging it along with the rest of your code base. With each release you may be adding/removing projects and consequently updating the TPS, so it’s important that the TPS matches what the repo looks like at that point. As projects are added/removed with each release, you have 3 possibilities: Recreate the TPS from an existing workspace: Same as the steps above, but it means that whoever does the export needs to maintain an up to date workspace to reflect the current project structure. Modify an existing TPS with the new/deleted project: This entails adding/removing an entry from the PSF file. Not a lot of maintenance, but someone needs to remember to do this. Automatically create/update the TPS: You could write a script that somehow updates the TPS to reflect the new repo structure. For example, if you’re developing an Eclipse RCP application, the PDE Build provides a map file that could be used as input to create the PSF file. If you want to checkout a branch other than trunk, just open the PSF file and do a Find/Replace of trunk with your branch name. You could also introduce an automated process as part of your build/release scripts to update the TPS with the correct branch and check it back in automatically, but that’s really optional. From http://eclipseone.wordpress.com
February 13, 2010
by Byron M
· 23,150 Views
article thumbnail
Facade Pattern Tutorial with Java Examples
Learn the Facade Design Pattern with easy Java source code examples as James Sugrue continues his design patterns tutorial series, Design Patterns Uncovered
February 12, 2010
by James Sugrue
· 217,838 Views · 9 Likes
article thumbnail
How to Manage Keyboard Shortcuts in Eclipse and Why You Should
when i use eclipse i try and use shortcuts all the time. they really make me work faster and give me time to focus on getting the code out rather than slowing down to invoke ide commands with the mouse. because people work differently and on different things, it’s important to manage keyboard shortcuts to suit the way you work. so it’s good that eclipse makes it really easy to change shortcuts and also to view shortcuts for the commands you use a lot. i’ll discuss how to change keyboard shortcuts, how to avoid conflicts with existing ones, why it’s a good thing to manage shortcuts and then end off with some examples of common shortcuts that you should add to you arsenal. how do you manage keyboard shortcuts the main preference page can be found under window > preferences > general > keys (or faster: press ctrl+3 , type keys and press enter ). from here you can see all commands and assign/change their associated keyboard shortcuts. in the video, i’ll show you how to reassign a key. we’ll use this to assign ctrl+tab to switch to the next editor. notes: you don’t have to copy a command to assign/reassign a shortcut. i just prefer to keep the old shortcut in case someone else wants to take over my keyboard and expects the shortcut to work. all commands registered with eclipse are listed on the keys preference page. browse through them or search to see if your favourite command is listed. if it doesn’t have a shortcut assigned, then assign one immediately. the when dropdown on the dialog shows you in which context the command applies (eg. only when editing java source). you can assign the same shortcut to two different commands, but if the context differs eclipse will only execute the one command. i suggest you don’t change the context. notice that eclipse shows you which keys conflict with the selected one if their in the same context. but all the keys are already taken no, they’re not. firstly, if a key you like is already taken then be creative. use combos like ctrl+alt, alt+shift or even alt+shift+ctrl (easier to press then you think). these aren’t used as often as ctrl or ctrl+shift. you can sort by binding to see which keys are already used. secondly, eclipse allows you to assign a sequence of keystrokes to a command. eg. alt+shift+x, j (that’s used to run the current class as a java application) is invoked by pressing alt+shift+x, releasing the keys then pressing j. if you forgot your sequence shortcuts, just press the first part (eg. alt+shift+x), wait a second then eclipse helps you out by listing all commands that have this keystroke as the first part of the sequence. the list appears in the lower right corner of the window. this works for any commands assigned a sequence of keystrokes. here’s an example of what you might see when you press the first sequence of keystrokes (notice the list in the lower, right corner). tip: for custom commands you want to assign, use a well-known first keystroke to group your commands. for example, i use alt+shift+b (b for byron) as my grouping prefix. this way there are fewer conflicts with existing shortcuts and eclipse can show me a quick list if i forgot which keys i used. so why bother managing keyboard shortcuts managing keyboard shortcuts in eclipse is important because: not all eclipse commands have assigned shortcuts. a command you frequently use could be assigned a shortcut. eg. an svn commit is registered as an eclipse command but doesn’t have a keyboard shortcut assigned. assigning one yourself will speed up checking in changes. sometime eclipse gets it wrong. a good example is ctrl+f6 for switching editors – ctrl+tab is a much more sensible option since the keys are close together and doable with one hand. going through the list of shortcuts may give you ideas for working faster or even show you a command you didn’t know eclipse could do. but don’t shortcuts take a long time to learn? no ways! i often hear people complain that it takes too long to learn the shortcuts. i assure you that the 5 minutes it takes to learn the shortcut saves you hours, even days, of work over the months and years that you’ll be working on eclipse. here are some tips to get into and improve your keyboard usage: start by learning the shortcuts for the things you do often. are you using the mouse a lot to launch applications or select code? well, there’s a shortcut to launch apps and one to select code faster. keyboard shortcuts are often indicated next to menu items. note them and try to use them instead of reaching for the mouse. learn to punish yourself when you reach for the mouse by undoing your action and forcing yourself to use the keyboard. this seems counterproductive (and harsh) at first, but it forces your brain to rewire itself so you’ll favour the shortcuts instead of the mouse the next time. there is an eclipse plugin called mousefeed that shows you a keyboard alternative to a mouse click you performed. you can also configure it to cancel mouse clicks where a keyboard alternative is available, forcing you to use the keyboard in those instances. i don’t use it personally but what i’ve seen looks good. it’s definitely sounds like a good idea if you want to change your mousey ways and it relates nicely to my previous point. also keep this in mind: one of the easiest, surefire ways to work faster is to reduce the number of times you move your hands between the keyboard and mouse. the key is doing as much as possible with either one. for the mouse there are things like mouse gestures (if you’re on windows, try strokeit ). for the keyboard there are keyboard shortcuts and eclipse has hundreds of them ready to make your life easier (also see the excellent autohotkey for other ways to boost your keyboard usage). examples of useful keyboard shortcuts there are way too many shortcuts to list and you’ll come across the more useful ones in other tips . here are some you should get acquainted with at first and try to use all the time. shortcut action ctrl+1 quick fix list (for resolving errors/warnings) and also refactoring ctrl+space display the autocomplete list to select a relevant method/template, etc. ctrl+3 open quick access which allows you to run commands and navigate views and dialogs by searching for them, similar to launchy on windows or quicksilver on mac. ctrl+shift+r open any resource in the workspace, eg. xml file, class file, etc. ctrl+shift+t open a java type, eg. a class or interface. f3 go to the declaration of the method/class/variable f11/ctrl+f11 debug/run the last launched application (see this tip for more information) ctrl+alt+h display all methods that call a method (call hierarchy) f2 show javadoc for the current element (shift+f2 shows external javadoc) alt+up/down, alt+shift+down, ctrl+d move a line up/down, copy a line, delete a line (see this tip for more information) ctrl+/ comment/uncomment the current line or selected lines. you can be anywhere in the line, not necessarily at the beginning. you can press ctrl+shift+l to get a list of registered keyboard shortcuts in the lower right corner. but for a complete list, i prefer to go to the preference dialog as i can see all commands that are registered, even if they don’t have shortcuts registered from http://eclipseone.wordpress.com
February 12, 2010
by Byron M
· 36,762 Views
article thumbnail
JavaFX, Sockets and Threading: Lessons Learned
When contemplating how machine-dependent applications might communicate with Java/JavaFX, JNI or the Java Native Interface, having been created for just such a task, would likely be the first mechanism that comes to mind. Although JNI works just fine thank you, a group of us ultimately decided against using it for a small project because, among others: Errors in your JNI implementation can corrupt the Java Virtual Machine in very strange ways, leading to difficult diagnosis. JNI can be time consuming and tedious, especially if there's a varied amount of interchange between the Native and Java platforms. For each OS/Platform supported, a separate JNI implementation would need to be created and maintained. Instead we opted for something a bit more mundane, namely sockets. The socket programming paradigm has been around a long time, is well understood and spans a multitude of hardware/software platforms. Rather than spending time defining JNI interfaces, just open up a socket between applications and send messages back and forth, defining your own message protocol. Following are some reflections on using sockets with JavaFX and Java. For the sake of simplicity, we'll skip the native stuff and focus on how sockets can be incorporated into a JavaFX application in a thread safe manner. Sockets and Threading Socket programming, especially in Java, lends itself to utilizing threads. Because a socket read() will block waiting for input, a common practice is to place the read loop in a background thread enabling you to continue processing while waiting for input at the same time. And if you're doing this work entirely in Java, you'll find that both ends of the socket connection -- the "server" side and the "client" side -- share a great deal of common code. Recognizing this, an abstract class called GenericSocket.java was created which is responsible for housing the common functionality shared by "server" and "client" sockets including the setup of a reader thread to handle socket reads asynchronously. For this simple example, two implementations of the abstract GenericSocket class, one called SocketServer.java, the other called SocketClient.java have been supplied. The primary difference between these two classes lies in the type of socket they use. SocketServer.java uses java.net.ServerSocket, while SocketClient.java uses java.net.Socket. The respective implementations contain the details required to set up and tear down these slightly different socket types. Dissecting the Java Socket Framework If you want to utilize the provided Java socket framework with JavaFX, you need to understand this very important fact: JavaFX is not thread safe and all JavaFX manipulation should be run on the JavaFX processing thread.1 If you allow a JavaFX application to interact with a thread other than the main processing thread, unpredictable errors will occur. Recall that the GenericSocket class created a reader thread to handle socket reads. In order to avoid non-main-thread-processing and its pitfalls with our socket classes, a few modifications must take place. [1] Stolen from JavaFX: Developing Rich Internet Applications - Thanks Jim Clarke Step 1: Identify Resources Off the Main Thread The first step to operating in a thread safe manner is to identify those resources in your Java code, residing off the main thread, that might need to be accessed by JavaFX. For our example, we define two abstract methods, the first, onMessage(), is called whenever a line of text is read from the socket. The GenericSocket.java code will make a call to this method upon encountering socket input. Let's take a look at the SocketReaderThread code inside GenericSocket, to get a feel for what's going on. class SocketReaderThread extends Thread { @Override public void run() { String line; waitForReady(); /* * Read from from input stream one line at a time */ try { if (input != null) { while ((line = input.readLine()) != null) { if (debugFlagIsSet(DEBUG_IO)) { System.out.println("recv> " + line); } /* * The onMessage() method has to be implemented by * a sublclass. If used in conjunction with JavaFX, * use Entry.deferAction() to force this method to run * on the main thread. */ onMessage(line); } } } catch (Exception e) { if (debugFlagIsSet(DEBUG_EXCEPTIONS)) { e.printStackTrace(); } } finally { notifyTerminate(); } } Because onMessage() is called off the main thread and inside SocketReaderThread, the comment states that some additional work, which we'll explain soon, must take place to assure main thread processing. Our second method, onClosedStatus(), is called whenever the status of the socket changes (either opened or closed for whatever reason). This abstract routine is called in different places within GenericSocket.java -- sometimes on the main thread, sometimes not. To assure thread safety, we'll employ the same technique as with onMessage(). Step 2: Create a Java Interface with your Identified Methods Once identified, these method signatures have to be declared inside a Java interface. For example, our socket framework includes a SocketListener.java interface file which looks like this: package genericsocket; public interface SocketListener { public void onMessage(String line); public void onClosedStatus(Boolean isClosed); } Step 3: Create Your Java Class, Implementing Your Defined Interface With our SocketListener interface defined, let's take a step-by-step look at how the SocketServer class is implemented inside SocketServer.java. One of the first requirements is to import a special Java class which will allow us to do main thread processing, achieved as follows: import com.sun.javafx.runtime.Entry; Next, comes the declaration of SocketServer. Notice that in addition to extending the abstract GenericSocket class it also must implement our SocketListener interface too: public class SocketServer extends GenericSocket implements SocketListener { Inside the SocketServer definition, a variable called fxListener of type SocketListener is declared: private SocketListener fxListener; The constructor for SocketServer must include a reference to fxListener. The other arguments are used to specify a port number and some debug flags. public SocketServer(SocketListener fxListener, int port, int debugFlags) { super(port, debugFlags); this.fxListener = fxListener; } Next, let's examine the implementation of the two methods which are declared in the SocketListener interface. The first, onMessage(), looks like this: /** * Called whenever a message is read from the socket. In * JavaFX, this method must be run on the main thread and * is accomplished by the Entry.deferAction() call. Failure to do so * *will* result in strange errors and exceptions. * @param line Line of text read from the socket. */ @Override public void onMessage(final String line) { Entry.deferAction(new Runnable() { @Override public void run() { fxListener.onMessage(line); } }); } As the comment points out, the Entry.deferAction() call enables fxListener.onMessage() to be executed on the main thread. It takes as an argument an instance of the Runnable class and, within its run() method, makes a call to fxListener.onMessage(). Another important point to notice is that onMessage()'s String argument must be declared as final. Along the same line, the onClosedStatus() method is implemented as follows: /** * Called whenever the open/closed status of the Socket * changes. In JavaFX, this method must be run on the main thread and * is accomplished by the Entry.deferAction() call. Failure to do so * will* result in strange errors and exceptions. * @param isClosed true if the socket is closed */ @Override public void onClosedStatus(final Boolean isClosed) { Entry.deferAction(new Runnable() { @Override public void run() { fxListener.onClosedStatus(isClosed); } }); } Another Runnable is scheduled via Entry.deferAction() to run fxlistener.onClosedStatus() on the main thread. Again, onClosedStatus()'s Boolean argument must also be defined as final. Accessing the Framework within JavaFX With this work behind us, now we can integrate the framework into JavaFX. But before elaborating on the details, lets show screenshots of two simple JavaFX applications, SocketServer and SocketClient which, when run together, can send and receive text messages to one another over a socket. These JavaFX programs were developed in NetBeans and utilize the recently announced NetBeans JavaFX Composer tool. You can click on the images to execute these programs via Java WebStart. Note: depending upon your platform, your system may ask for permission prior to allowing these applications to network. Source for the JavaFX applications and the socket framework in the form of NetBeans projects can be downloaded here. Step 4: Integrating into JavaFX To access the socket framework within JavaFX, you must implement the SocketListener class that was created for this project. To give you a feel for how this is done with our JavaFX SocketServer application, here are some code excerpts from the project's Main.fx file, in particular the definition of our ServerSocketListener class: public class ServerSocketListener extends SocketListener { public override function onMessage(line: String) { insert line into recvListView.items; } public override function onClosedStatus(isClosed : java.lang.Boolean) { socketClosed = isClosed; tryingToConnect = false; if (autoConnectCheckbox.selected) { connectButtonAction(); } } } Sparing all of the gory details, the onMessage() method will place the line of text read from the socket in to a JavaFX ListView control which is displayed in the program user interface. The onClosedStatus() method primarily updates the local socketClosed variable and attempts to reconnect the socket if the autoconnect option has been selected. To demonstrate how the socket is created, we examine the connectButtonAction() function: var socketServer : SocketServer; ... public function connectButtonAction (): Void { if (not tryingToConnect) { if (socketClosed) { socketServer = new SocketServer(ServerSocketListener{}, java.lang.Integer.parseInt(portTextbox.text), javafx.util.Bits.bitOr(GenericSocket.DEBUG_STATUS, GenericSocket.DEBUG_IO)); tryingToConnect = true; socketServer.connect(); } } } Whenever the user clicks on the "Connect" button, the connectButtonAction() function will be called. On invocation, if the socket isn't already open, it will create a new SocketServer instance. Recognize also that the SocketServer constructor includes an instance of the ServerSocketListener class which was defined above. To round this out, when the user clicks on the "Disconnect" button, the disconnectButtonAction() function is called. When invoked, it tears down the SocketServer instance. function disconnectButtonAction (): Void { tryingToConnect = false; socketServer.shutdown(); } Conclusion Admittedly, there's a fair amount to digest here. Hopefully, by carefully reviewing the steps and looking at the complete code listing, this can serve as a template if you wish to accomplish something similar in JavaFX. From http://blogs.sun.com/jtc/
February 11, 2010
by Jim Connors
· 22,272 Views
article thumbnail
Promiscuous Integration vs. Continuous Integration
The emergence of version control systems makes both promiscuous and continuous integration merging techniques more attractive. Which is better?
February 10, 2010
by Martin Fowler
· 50,299 Views · 2 Likes
article thumbnail
Adapter Pattern Tutorial with Java Examples
Learn the Adapter Design Pattern with easy Java source code examples as James Sugrue continues his design patterns tutorial series, Design Patterns Uncovered
February 9, 2010
by James Sugrue
· 223,818 Views · 3 Likes
article thumbnail
New in Groovy 1.7.1: Constructor Mocking and Half Mocks
The Groovy MockFor object got some fun new features this weekend: constructor mocking and "half-mocks". The tickets are marked for Groovy 1.7.1, so these features are available in the nightly builds or in the next few weeks as 1.7.1 is released. The easiest thing is, of course, to just build from source. The first feature is Constructor mocking. It is now possible to specify a return value for mock constructor calls. Here is an example that uses a "Person" object. The "tom" variable is a real Person object, while the "mary" object is not. Thanks to this new feature, instantiating Mary returns to you Tom. import groovy.mock.interceptor.MockFor class Person { String name } def tom = new Person(name:'Tom') def mock = new MockFor(Person, true) mock.demand.with { Person() { tom } } mock.use { def mary = new Person(name:'Mary') assert tom == mary } There are two parts to getting this working: the "demand.with" block, where the person constructor is specified to return tom, and the true parameter passed to MockFor. To make MockFor backwards compatible, a flag was needed to control when to allow constructor mocking and when not to allow it. As you can see, after doing this, the tom and mary objects are equal (in fact, the same reference). The problem with this feature alone is that calling tom.getName() results in a mock exception saying no demand is set. If Tom is a real instance of Person then why would you get a demand exception? Well, within the mock.use block Tom is a mock. The behaviour you probably expect is to have the real Tom methods pass through to the real implementation, which is why "half-mocks" were introduced. Check out how you can specify methods to ignore and pass through to the underlying object: mock.ignore(~'get.*') mock.demand.with { Person() { tom } } mock.use { def mary = new Person(name:'Mary') assert mary.name == 'Tom' } The ignore method takes a pattern to match, and the mock ignores those methods, passing them on to the underlying implementation. So your mock is still a mock, not a real object, but methods are passed through as needed. This is the opposite of how EasyMock partial mocking works, where a partial mock is technically a subclass of the target class, and methods are routed by default to the real implementation. In Groovy, the method are routed by default to the mock. Fun stuff, and thanks to Paul King for the work! From http://hamletdarcy.blogspot.com
February 8, 2010
by Hamlet D'Arcy
· 17,245 Views
article thumbnail
Observer Pattern Tutorial with Java Examples
Learn the Observer Design Pattern with easy Java source code examples as James Sugrue continues his design patterns tutorial series, Design Patterns Uncovered
February 3, 2010
by James Sugrue
· 174,138 Views · 10 Likes
article thumbnail
Memory Leak Protection in Tomcat 7
In this exclusive interview with DZone, Thomas elaborates on these fixes coming in the next version of Apache Tomcat.
February 1, 2010
by Mitch Pronschinske
· 86,613 Views · 1 Like
article thumbnail
Reloading Java Classes: Classloaders in Web Development — Tomcat, GlassFish, OSGi, Tapestry 5
In this article we’ll review how dynamic classloaders are used in real servers, containers and frameworks to reload Java classes and applications. We’ll also touch on how to get faster reloads and redeploys by using them in optimal ways. RJC101: Objects, Classes and ClassLoaders RJC201: How do ClassLoader leaks happen? RJC301: Classloaders in Web Development — Tomcat, GlassFish, OSGi, Tapestry 5 and so on RJC401: HotSwap and JRebel — what do they really do? RJC501: The impact of the redeploy phase on the development process AKA:Turnaround Java EE (web) applications In order for a Java EE web application to run, it has to be packaged into an archive with a .WAR extension and deployed to a servlet container like Tomcat. This makes sense in production, as it gives you a simple way to assemble and deploy the application, but when developing that application you usually just want to edit the application’s files and see the changes in the browser. A Java EE enterprise application has to be packaged into an archive with an .EAR extension and deployed to an application container. It can contain multiple web applications and EJB modules, so it often takes a while to assemble and deploy it. Recently, 1100+ EE developers told us how much time it takes them, and we compiled the results into the Redeploy and Restart Report. Spoiler: Avg redeploy & restart time is 2.5 minutes – which is higher than we expected. In Reloading Java Classes 101, we examined how dynamic classloaders can be used to reload Java classes and applications. In this article we will take a look at how servers and frameworks use dynamic classloaders to speed up the development cycle. We’ll use Apache Tomcat as the primary example and comment when behavior differs in other containers (Tomcat is also directly relevant for JBoss and GlassFish as these containers embed Tomcat as the servlet container). Redeployment To make use of dynamic classloaders we must first create them. When deploying your application, the server will create one classloader for each application (and each application module in the case of an enterprise application). The classloaders form a hierarchy as illustrated: In Tomcat each .WAR application is managed by an instance of the StandardContext class that creates an instance of WebappClassLoader used to load the web application classes. When a user presses “reload” in the Tomcat Manager the following will happen: StandardContext.reload() method is called The previous WebappClassLoader instance is replaced with a new one All reference to servlets are dropped New servlets are created Servlet.init() is called on them Calling Servlet.init() recreates the “initialized” application state with the updated classes loaded using the new classloader instance. The main problem with this approach is that to recreate the “initialized” state we run the initialization from scratch, which usually includes loading and processing metadata/configuration, warming up caches, running all kinds of checks and so on. In a sufficiently large application this can take many minutes, but in a in small application this often takes just a few seconds and is fast enough to seem instant, as commonly demonstrated in the Glassfish v3 promotional demos. If your application is deployed as an .EAR archive, many servers allow you to also redeploy each application module separately, when it is updated. This saves you the time you would otherwise spend waiting for non-updated modules to reinitialize after the redeployment. Hot Deployment Web containers commonly have a special directory (e.g. “webapps” in Tomcat, “deploy” in JBoss) that is periodically scanned for new web applications or changes to the existing ones. When the scanner detects that a deployed .WAR is updated, the scanner causes a redeploy to happen (in Tomcat it calls the StandardContext.reload() method). Since this happens without any additional action on the user’s side it is commonly referred to “Hot Deployment”. Hot Deployment is supported by all wide-spread application servers under different names: autodeployment, rapid deployment, autopublishing, hot reload, and so on. In some containers, instead of moving the archive to a predefined directory you can configure the server to monitor the archive at a specific path. Often the redeployment can be triggered from the IDE (e.g. when the user saves a file) thus reloading the application without any additional user involvement. Although the application is reloaded transparently to the user, it still takes the same amount of time as when hitting the “Reload” button in the admin console, so code changes are not immediately visible in the browser, for example. Another problem with redeployment in general and hot deployment in particular is classloader leaks. As we reviewed in Reloading Java Classes 201, it is amazingly easy to leak a classloader and quickly run out of heap causing an OutOfMemoryError. As each deployment creates new classloaders, it is common to run out of memory in just a few redeploys on a large enough application (whether in development or in production). Exploded Deployment An additional feature supported by the majority of web containers is the so called “exploded deployment”, also known as “unpackaged” or “directory” deployment. Instead of deploying a .WAR archive, one can deploy a directory with exactly the same layout as the .WAR archive: Why bother? Well, packaging an archive is an expensive operation, so deploying the directory can save quite a bit of time during build. Moreover, it is often possible to set up the project directory with exactly the same layout as the .WAR archive. This means an added benefit of editing files in place, instead of copying them to the server. Unfortunately, as Java classes cannot be reloaded without a redeploy, changing a .java file still means waiting for the application to reinitialize. With some servers it makes sense to find out exactly what triggers the hot redeploy in the exploded directory. Sometimes the redeploy will be triggered only when the “web.xml” timestamp changes, or as in the case of GlassFish only when a special ”.reload” file timestamp changes. In most servers any change to deployment descriptors or compiled classes will cause a hot redeploy. If your server only supports deploying by copying to a special directory (e.g. Tomcat “webapps”, JBoss “deploy” directories) you can skip the copying by creating a symlink from that special directory to your project workspace. On Linux and Mac OS X you can use the common “ln -s” command to do that, whereas on Windows you should download the Sysinternals “junction” utility. If you use Maven, then it’s quite complicated to set up exploded development from your workspace. If you have a solo web application you can use the Maven Jetty plugin, which uses classes and resources directly from Maven source and target project directories. Unfortunately, the Maven Jetty plugin does not support deploying multiple web applications, EJB modules or EARs so in the latter case you’re stuck doing artifact builds. Session Persistence Since we’re on the topic of reloading classes, and redeploying involves reinitializing an application, it makes sense to talk about session state. An HTTP session usually holds information like login credentials and conversational state. Losing that session when developing a web application means spending time logging in and browsing to the changes page – something that most web containers have tried to solve by serializing all of the objects in the HttpSession map and then deserializing them in the new classloader. Essentially, they copy all of the session state. This requires that all session attributes implement Serializable (ensuring session attributes can be written to a database or a file for later use), which is not restricting in most cases. Session persistence has been present in most major containers for many years (e.g. Restart Persistence in Tomcat), but was notoriously absent in Glassfish before v3. OSGi There is a lot of misunderstanding surrounding what exactly OSGi does and doesn’t do. If we ignore the aspects irrelevant to the current issue, OSGi is basically a collection of modules each wrapped in its own classloader, which can be dropped and recreated at will. When it’s recreated, the modules are reinitialized exactly the same way a web application is. The difference between OSGi and a web container is that OSGi is something that is exposed to your application, that you use to split your application into arbitrarily small modules. Therefore, by design, these modules will likely be much smaller than the monolithic web applications we are used to building. And since each of these modules is smaller and we can “redeploy” them one-by-one, re-initialization takes less time. The time depends on how you design your application (and can still be significant). Tapestry 5, RIFE & Grails Recently, some web frameworks, such as Tapestry 5, RIFE and Grails, have taken a different approach, taking advantage of the fact that they already need to maintain application state. They’ll ensure that state will be serializable, or otherwise easily re-creatable, so that after dropping a classloader, there is no need to reinitialize anything. This means that application developers use frameworks’ components and the lifecycle of those components is handled by the framework. The framework will initialize (based on some configuration, either xml or annotation based), run and destroy the components. As the lifecycle of the components is managed by the framework, it is easy to recreate a component in a new classloader without user intervention and thus create the effect of reloading code. In the background, the old component is destroyed (classloader is dropped) and a new one created (in a new classloader where the classes are read in again) and the old state is either deserialized or created based on the configuration. This has the obvious advantage of being very quick, as components are small and the classloaders are granular. Therefore the code is reloaded instantly, giving a smooth experience in developing the application. However such an approach is not always possible as it requires the component to be completely managed by the framework. It also leads to incompatibilities between the different class versions causing, among others, ClassCastExceptions. We’ve Covered a Lot – and simplified along the way It’s worth mentioning that using classloaders for code reloading really isn’t as smooth as we have described here – this is an introductory article series. Especially with the more granular approaches (such as frameworks that have per component classloaders, manual classloader dropping and recreating, etc), when you start getting a mixture of older and newer classes all hell can break loose. You can hold all kinds of references to old objects and classes, which will conflict with the newly loaded ones (a common problem is getting a ClassCastException), so watch what you’re doing along the way. As a side note: Groovy is actually somewhat better at handling this, as all calls through the Meta-Object Protocol are not subject to such problems. This article addressed the following questions: How are dynamic classloaders used to reload Java classes and applications? How do Tomcat, GlassFish (incl v3), and other servers reload Java classes and applications? How does OSGi improve reload and redeploy times? How do frameworks (incl Tapestry 5, RIFE, Grails) reload Java classes and applications? Coming up next, we continue our explanation of classloaders and the redeploy process with an investigation into HotSwap and JRebel, two tools used to reduce time spent reloading and redeploying. Stay tuned! From http://www.zeroturnaround.com/blog/
January 23, 2010
by Dave Booth
· 26,436 Views
article thumbnail
Useful U.S. Airport/City Codes In HTML Drop Down List Format
// Drop down list of Airport code in HTML. Sorted Alphabetically. Aberdeen, SD (ABR) Abilene, TX (ABI) Adak Island, AK (ADK) Akiachak, AK (KKI) Akiak, AK (AKI) Akron/Canton, OH (CAK) Akuton, AK (KQA) Alakanuk, AK (AUK) Alamogordo, NM (ALM) Alamosa, CO (ALS) Albany, NY (ALB) Albany, OR - Bus service (CVO) Albany, OR - Bus service (QWY) Albuquerque, NM (ABQ) Aleknagik, AK (WKK) Alexandria, LA (AEX) Allakaket, AK (AET) Allentown, PA (ABE) Alliance, NE (AIA) Alpena, MI (APN) Altoona, PA (AOO) Amarillo, TX (AMA) Ambler, AK (ABL) Anaktueuk, AK (AKP) Anchorage, AK (ANC) Angoon, AK (AGN) Aniak, AK (ANI) Anvik, AK (ANV) Appleton, WI (ATW) Arcata, CA (ACV) Arctic Village, AK (ARC) Asheville, NC (AVL) Ashland, KY/Huntington, WV (HTS) Aspen, CO (ASE) Athens, GA (AHN) Atka, AK (AKB) Atlanta, GA (ATL) Atlantic City, NJ (AIY) Atqasuk, AK (ATK) Augusta, GA (AGS) Augusta, ME (AUG) Austin, TX (AUS) Bakersfield, CA (BFL) Baltimore, MD (BWI) Bangor, ME (BGR) Bar Harbour, ME (BHB) Barrow, AK (BRW) Barter Island, AK (BTI) Baton Rouge, LA (BTR) Bay City, MI (MBS) Beaumont/Port Arthur, TX (BPT) Beaver Creek, CO - Van service (ZBV) Beaver, AK (WBQ) Beckley, WV (BKW) Bedford, MA (BED) Belleville, IL (BLV) Bellingham, WA (BLI) Bemidji, MN (BJI) Benton Harbor, MI (BEH) Bethel, AK (BET) Bethlehem, PA (ABE) Bettles, AK (BTT) Billings, MT (BIL) Biloxi/Gulfport, MS (GPT) Binghamton, NY (BGM) Birch Creek, AK (KBC) Birmingham, AL (BHM) Bismarck, ND (BIS) Block Island, RI (BID) Bloomington, IL (BMI) Bluefield, WV (BLF) Boise, ID (BOI) Boston, MA (BOS) Boulder, CO - Bus service (XHH) Boulder, CO - Hiltons Har H (WHH) Boulder, CO - Municipal Airport (WBU) Boundary, AK (BYA) Bowling Green, KY (BWG) Bozeman, MT (BZN) Bradford, PA (BFD) Brainerd, MN (BRD) Brawnwood, TX (BWD) Breckenridge, CO - Van service (QKB) Bristol, VA (TRI) Brookings, SD (BKX) Brooks Lodge, AK (RBH) Brownsville, TX (BRO) Brunswick, GA (BQK) Buckland, AK (BKC) Buffalo, NY (BUF) Bullhead City/Laughlin, AZ (IFP) Burbank, CA (BUR) Burlington, IA (BRL) Burlington, VT (BTV) Butte, MT (BTM) Canton/Akron, OH (CAK) Cape Girardeau, MO (CGI) Cape Lisburne, AK (LUR) Cape Newenham, AK (EHM) Carbondale, IL (MDH) Carlsbad, CA (CLD) Carlsbad, NM (CNM) Carmel, CA (MRY) Casper, WY (CPR) Cedar City, UT (CDC) Cedar Rapids, IA (CID) Central, AK (CEM) Chadron, NE (CDR) Chalkyitsik, AK (CIK) Champaign/Urbana, IL (CMI) Charleston, SC (CHS) Charleston, WV (CRW) Charlotte, NC (CLT) Charlottesville, VA (CHO) Chattanooga, TN (CHA) Chefornak, AK (CYF) Chevak, AK (VAK) Cheyenne, WY (CYS) Chicago, IL - All airports (CHI) Chicago, IL - Midway (MDW) Chicago, IL - O'Hare (ORD) Chicken, AK (CKX) Chico, CA (CIC) Chignik, AK - Fisheries (KCG) Chignik, AK - (KCQ) Chignik, AK - Lagoon (KCL) Chisana, AK (CZN) Chisholm/Hibbing, MN (HIB) Chuathbaluk, AK (CHU) Cincinnati, OH (CVG) Circle Hot Springs, AK (CHP) Circle, AK (IRC) Clarks Point, AK (CLP) Clarksburg, WV (CKB) Clearwater/St Petersburg, FL (PIE) Cleveland, OH (CLE) Clovis, NM (CVN) Cody/Yellowstone, WY (COD) Coffee Point, AK (CFA) Coffman Cove, AK (KCC) Cold Bay, AK (CDB) College Station, TX (CLL) Colorado Springs, CO (COS) Columbia, MO (COU) Columbia, SC (CAE) Columbus, GA (CSG) Columbus, MS (GTR) Columbus, OH (CMH) Concord, CA (CCR) Concordia, KS (CNK) Copper Mountain, CO - Van service (QCE) Cordova, AK (CDV) Corpus Christi, TX (CRP) Cortez, CO (CEZ) Craig, AK (CGA) Crescent City, CA (CEC) Crooked Creek, AK (CKO) Cube Cove, AK (CUW) Cumberland, MD (CBE) Dallas, TX - Love Field (DAL) Dallas, TX - Dallas/Ft Worth Intl. (DFW) Dayton, OH (DAY) Daytona Beach, FL (DAB) Decatur, IL (DEC) Deering, AK (DRG) Del Reo, TX (DRT) Delta Junction, AK (DJN) Denver, CO - International (DEN) Denver, CO - Longmont Bus service (QWM) Des Moines, IA (DSM) Detroit, MI - All airports (DTT) Detroit, MI - Metro/Wayne County (DTW) Devil's Lake, ND (DVL) Dickinson, ND (DIK) Dillingham, AK (DLG) Dodge City, KS (DDC) Dothan, AL (DHN) Dubois, PA (DUJ) Dubuque, IA (DBQ) Duluth, MN (DLH) Durango, CO (DRO) Durham, NC (RDU) Durham/Raleigh, NC (RDU) Dutch Harbor, AK (DUT) Easton, PA (ABE) Eau Claire, WI (EAU) Edna Bay, AK (EDA) Eek, AK (EEK) Ekuk, AK (KKU) Ekwok, AK (KEK) El Centro, CA (IPL) El Dorado, AR (ELD) El Paso, TX (ELP) Elfin Cove, AK (ELV) Elim, AK (ELI) Elko, NV (EKO) Elmira, NY (ELM) Ely, MN (LYU) Emmonak, AK (EMK) Endicott, NY (BGM) Enid, OK (WDG) Erie, PA (ERI) Escanaba, MI (ESC) Eugene, OR (EUG) Eureka/Arcata, CA (ACV) Eureka, NV (EUE) Evansville, IN (EVV) Fairbanks, AK (FAI) Fargo, ND (FAR) Farmington, NM (FMN) Fayetteville, AR - Municipal/Drake (FYV) Fayetteville, AR - Northwest Arkansas Regional (XNA) Fayetteville, NC (FAY) Flagstaff, AZ (FLG) Flint, MI (FNT) Florence, SC (FLO) Florence/Muscle Shoals/Sheffield, AL (MSL) Fort Collins/Loveland, CO - Municipal Airport (FNL) Fort Collins/Loveland, CO - Bus service (QWF) Fort Dodge, IA (FOD) Fort Lauderdale, FL (FLL) Fort Leonard Wood, MO (TBN) Fort Myers, FL (RSW) Fort Smith, AR (FSM) Fort Walton Beach, FL (VPS) Fort Wayne, IN (FWA) Fort Worth/Dallas, TX (DFW) Franklin, PA (FKL) Fresno, CA (FAT) Gainesville, FL (GNV) Gallup, NM (GUP) Garden City, KS (GCK) Gary, IN (GYY) Gillette, WY (GCC) Gladewater/Kilgore, TX (GGG) Glasgow, MT (GGW) Glendive, MT (GDV) Golovin, AK (GLV) Goodnews Bay, AK (GNU) Grand Canyon, AZ - Heliport (JGC) Grand Canyon, AZ - National Park (GCN) Grand Forks, ND (GFK) Grand Island, NE (GRI) Grand Junction, CO (GJT) Grand Rapids, MI (GRR) Grand Rapids, MN (GPZ) Grayling, AK (KGX) Great Falls, MT (GTF) Green Bay, WI (GRB) Greensboro, NC (GSO) Greenville, MS (GLH) Greenville, NC (PGV) Greenville/Spartanburg, SC (GSP) Groton/New London, CT (GON) Gulfport, MS (GPT) Gunnison, CO (GUC) Gustavus, AK (GST) Hagerstown, MD (HGR) Hailey, ID (SUN) Haines, AK (HNS) Hampton, VA (PHF) Hana, HI - Island of Maui (HNM) Hanapepe, HI (PAK) Hancock, MI (CMX) Hanover, NH (LEB) Harlingen, TX (HRL) Harrisburg, PA (MDT) Harrison, AR (HRO) Hartford, CT (BDL) Havasupai, AZ (HAE) Havre, MT (HVR) Hayden, CO (HDN) Hays, KS (HYS) Healy Lake, AK (HKB) Helena, MT (HLN) Hendersonville, NC (AVL) Hibbing/Chisholm, MN (HIB) Hickory, NC (HKY) High Point, NC (GSO) Hilo, HI - Island of Hawaii (ITO) Hilton Head, SC (HHH) Hobbs, NM (HBB) Hollis, AK (HYL) Holy Cross, AK (HCR) Homer, AK (HOM) Honolulu, HI - Island of Oahu (HNL) Hoolehua, HI - Island of Molokai (MKK) Hoonah, AK (HNH) Hooper Bay, AK (HPB) Hot Springs, AR (HOT) Houston, TX - All airports (HOU) Houston, TX - Hobby (HOU) Houston, TX - Intercontinental (IAH) Hughes, AK (HUS) Huntington, WV/Ashland, KY (HTS) Huntsville, AL (HSV) Huron, SD (HON) Huslia, AK (HSL) Hyannis, MA (HYA) Hydaburg, AK (HYG) Idaho Falls, ID (IDA) Igiugig, AK (IGG) Iliamna, AK (ILI) Imperial, CA (IPL) Indianapolis, IN (IND) International Falls, MN (INL) Inyokern, CA (IYK) Iron Mountain, MI (IMT) Ironwood, MI (IWD) Islip, NY (ISP) Ithaca, NY (ITH) Jackson Hole, WY (JAC) Jackson, MS (JAN) Jackson, TN (MKL) Jacksonville, FL (JAX) Jacksonville, NC (OAJ) Jamestown, ND (JMS) Jamestown, NY (JHW) Janesville, WI (JVL) Johnson City, NY (BGM) Johnson City, TN (TRI) Johnstown, PA (JST) Jonesboro, AR (JBR) Joplin, MO (JLN) Juneau, AK (JNU) Kahului, HI - Island of Maui, (OGG) Kake, AK (KAE) Kakhonak, AK (KNK) Kalamazoo, MI (AZO) Kalaupapa, HI - Island of Molokai, (LUP) Kalskag, AK (KLG) Kaltag, AK (KAL) Kamuela, HI - Island of Hawaii, (MUE) Kansas City, MO (MCI) Kapalua, HI - Island of Maui, (JHM) Kasaan, AK (KXA) Kasigluk, AK (KUK) Kauai Island/Lihue, HI (LIH) Kearney, NE (EAR) Keene, NH (EEN) Kenai, AK (ENA) Ketchikan, AK (KTN) Key West, FL (EYW) Keystone, CO - Van service (QKS) Kiana, AK (IAN) Kilgore/Gladewater, TX (GGG) Killeen, TX (ILE) King Cove, AK (KVC) King Salmon, AK (AKN) Kingman, AZ (IGM) Kingsport, TN (TRI) Kipnuk, AK (KPN) Kirksville, MO (IRK) Kivalina, AK (KVL) Klamath Falls, OR (LMT) Klawock, AK (KLW) Knoxville, TN (TYS) Kobuk, AK (OBU) Kodiak, AK (ADQ) Kona, HI - Island of Hawaii (KOA) Kongiganak, AK (KKH) Kotlik, AK (KOT) Kotzebue, AK (OTZ) Koyukuk, AK (KYU) Kwethluk, AK (KWT) Kwigillingok, AK (KWK) La Crosse, WI (LSE) Lafayette, IN (LAF) Lafayette, LA (LFT) Lake Charles, LA (LCH) Lake Havasu City, AZ (HII) Lake Minchumina, AK (LMA) Lanai City, HI - Island of Lanai (LNY) Lancaster, PA (LNS) Lansing, MI (LAN) Laramie, WY (LAR) Laredo, TX (LRD) Las Vegas, NV (LAS) Latrobe, PA (LBE) Laurel, MS (PIB) Lawton, OK (LAW) Lebanon, NH (LEB) Levelock, AK (KLL) Lewisburg, WV (LWB) Lewiston, ID (LWS) Lewistown, MT (LWT) Lexington, KY (LEX) Liberal, KS (LBL) Lihue, HI - Island of Kaui (LIH) Lincoln, NE (LNK) Little Rock, AR (LIT) Long Beach, CA (LGB) Longview, TX (GGG) Lopez Island, WA (LPS) Los Angeles, CA (LAX) Louisville, KY (SDF) Loveland/Fort Collins, CO - Municipal Airport (FNL) Loveland/Fort Collins, CO - Bus service (QWF) Lubbock, TX (LBB) Macon, GA (MCN) Madison, WI (MSN) Madras, OR (MDJ) Manchester, NH (MHT) Manhattan, KS (MHK) Manistee, MI (MBL) Mankato, MN (MKT) Manley Hot Springs, AK (MLY) Manokotak, AK (KMO) Marietta, OH/Parkersburg, WV (PKB) Marion, IL (MWA) Marquette, MI (MQT) Marshall, AK (MLL) Martha's Vineyard, MA (MVY) Martinsburg, PA (AOO) Mason City, IA (MCW) Massena, NY (MSS) Maui, HI (OGG) Mcallen, TX (MFE) Mccook, NE (MCK) Mcgrath, AK (MCG) Medford, OR (MFR) Mekoryuk, AK (MYU) Melbourne, FL (MLB) Memphis, TN (MEM) Merced, CA (MCE) Meridian, MS (MEI) Metlakatla, AK (MTM) Meyers Chuck, AK (WMK) Miami, FL - International (MIA) Miami, FL - Sea Plane Base (MPB) Midland, MI (MBS) Midland/Odessa, TX (MAF) Miles City, MT (MLS) Milwaukee, WI (MKE) Minneapolis, MN (MSP) Minot, ND (MOT) Minto, AK (MNT) Mission, TX (MFE) Missoula, MT (MSO) Moab, UT (CNY) Mobile, AL (MOB) Modesto, CA (MOD) Moline, IL (MLI) Monroe, LA (MLU) Monterey, CA (MRY) Montgomery, AL (MGM) Montrose, CO (MTJ) Morgantown, WV (MGW) Moses Lake, WA (MWH) Mountain Home, AR (WMH) Mountain Village, AK (MOU) Muscle Shoals, AL (MSL) Muskegon, MI (MKG) Myrtle Beach, SC (MYR) Nantucket, MA (ACK) Napakiak, AK (WNA) Napaskiak, AK (PKA) Naples, FL (APF) Nashville, TN (BNA) Naukiti, AK (NKI) Nelson Lagoon, AK (NLG) New Chenega, AK (NCN) New Haven, CT (HVN) New Koliganek, AK (KGK) New London/Groton (GON) New Orleans, LA (MSY) New Stuyahok, AK (KNW) New York, NY - All airports (NYC) New York, NY - Kennedy (JFK) New York, NY - La Guardia (LGA) Newark, NJ (EWR) Newburgh/Stewart Field, NY (SWF) Newport News, VA (PHF) Newtok, AK (WWT) Nightmute, AK (NME) Nikolai, AK (NIB) Nikolski, AK (IKO) Noatak, AK (WTK) Nome, AK (OME) Nondalton, AK (NNL) Noorvik, AK (ORV) Norfolk, NE (OFK) Norfolk, VA (ORF) North Bend, OR (OTH) North Platte, NE (LBF) Northway, AK (ORT) Nuiqsut, AK (NUI) Nulato, AK (NUL) Nunapitchuk, AK (NUP) Oakland, CA (OAK) Odessa/Midland, TX (MAF) Ogdensburg, NY (OGS) Oklahoma City, OK (OKC) Omaha, NE (OMA) Ontario, CA (ONT) Orange County, CA (SNA) Orlando, FL - Herndon (ORL) Orlando, FL - International (MCO) Oshkosh, WI (OSH) Ottumwa, IA (OTM) Owensboro, KY (OWB) Oxnard/Ventura, CA (OXR) Paducah, KY (PAH) Page, AZ (PGA) Palm Springs, CA (PSP) Panama City, FL (PFN) Parkersburg, WV/Marietta, OH (PKB) Pasco, WA (PSC) Pedro Bay, AK (PDB) Pelican, AK (PEC) Pellston, MI (PLN) Pendleton, OR (PDT) Pensacola, FL (PNS) Peoria, IL (PIA) Perryville, AK (KPV) Petersburg, AK (PSG) Philadelphia, PA - International (PHL) Philadelphia, PA - Trenton/Mercer NJ (TTN) Phoenix, AZ (PHX) Pierre, SD (PIR) Pilot Point, AK - Ugashnik Bay (UGB) Pilot Point, AK (PIP) Pilot Station, AK (PQS) Pittsburgh, PA (PIT) Platinum, AK (PTU) Plattsburgh, NY (PLB) Pocatello, ID (PIH) Point Baker, AK (KPB) Point Hope, AK (PHO) Point Lay, AK (PIZ) Ponca City, OK (PNC) Ponce, Puerto Rico (PSE) Port Alsworth, AK (PTA) Port Angeles, WA (CLM) Port Arthur/Beaumont, TX (BPT) Port Clarence, AK (KPC) Port Heiden, AK (PTH) Port Moller, AK (PML) Port Protection, AK (PPV) Portage Creek, AK (PCA) Portland, ME (PWM) Portland, OR (PDX) Portsmouth, NH (PSM) Poughkeepsie, NY (POU) Prescott, AZ (PRC) Presque Isle, ME (PQI) Princeton, WV (BLF) Providence, RI (PVD) Provincetown, MA (PVC) Prudhoe Bay/Deadhorse, AK (SCC) Pueblo, CO (PUB) Pullman, WA (PUW) Quincy, IL (UIN) Quinhagak, AK (KWN) Raleigh/Durham, NC (RDU) Rampart, AK (RMP) Rapid City, SD (RAP) Reading, PA (RDG) Red Devil, AK (RDV) Redding, CA (RDD) Redmond, OR (RDM) Reno, NV (RNO) Rhinelander, WI, (RHI) Richmond, VA (RIC) Riverton, WY (RIW) Roanoke, VA (ROA) Roche Harbor, WA (RCE) Rochester, MN (RST) Rochester, NY (ROC) Rock Springs, WY (RKS) Rockford, IL (RFD) Rockland, ME (RKD) Rosario, WA (RSJ) Roswell, NM (ROW) Ruby, AK (RBY) Russian Mission, AK (RSH) Rutland, VT (RUT) Sacramento, CA (SMF) Saginaw, MI (MBS) Saint Cloud, MN (STC) Saint George Island, AK (STG) Saint George, UT (SGU) Saint Louis, MO (STL) Saint Mary's, AK (KSM) Saint Michael, AK (SMK) Saint Paul Island, AK (SNP) Salem, OR (SLE) Salina, KS (SLN) Salisbury-Ocean City, MD (SBY) Salt Lake City, UT (SLC) San Angelo, TX (SJT) San Antonio, TX (SAT) San Diego, CA (SAN) San Francisco, CA (SFO) San Jose, CA (SJC) San Juan, Puerto Rico (SJU) San Luis Obispo, CA (SBP) Sand Point, AK (SDP) Sanford, FL (SFB) Santa Ana, CA (SNA) Santa Barbara, CA (SBA) Santa Fe, NM (SAF) Santa Maria, CA (SMX) Santa Rosa, CA (STS) Saranac Lake, NY (SLK) Sarasota, FL (SRQ) Sault Ste Marie, MI, (CIU) Savannah, GA (SAV) Savoonga, AK (SVA) Scammon Bay, AK (SCM) Scottsbluff, NE (BFF) Scranton, PA (AVP) Seattle, WA - Lake Union SPB (LKE) Seattle, WA - Seattle/Tacoma International (SEA) Selawik, AK (WLK) Seward, AK (SWD) Shageluk, AK (SHX) Shaktoolik, AK (SKK) Sheffield/Florence/Muscle Shoals, AL (MSL) Sheldon Point, AK (SXP) Sheridan, WY (SHR) Shishmaref, AK (SHH) Shreveport, LA (SHV) Shungnak, AK (SHG) Silver City, NM (SVC) Sioux City, IA (SUX) Sioux Falls, SD (FSD) Sitka, AK (SIT) Skagway, AK (SGY) Sleetmore, AK (SLQ) South Bend, IN (SBN) South Naknek, AK (WSN) Southern Pines, NC (SOP) Spartanburg/Greenville, SC (GSP) Spokane, WA (GEG) Springfield, IL (SPI) Springfield, MO (SGF) St Petersburg/Clearwater, FL (PIE) State College/University Park, PA (SCE) Staunton, VA (SHD) Steamboat Springs, CO (SBS) Stebbins, AK (WBB) Stevens Point/Wausau, WI (CWA) Stevens Village, AK (SVS) Stewart Field/Newburgh, NY (SWF) Stockton, CA (SCK) Stony River, AK (SRV) Sun Valley, ID (SUN) Syracuse, NY (SYR) Takotna, AK (TCT) Talkeetna, AK (TKA) Tallahassee, FL (TLH) Tampa, FL (TPA) Tanana, AK (TAL) Taos, NM (TSM) Tatitlek, AK (TEK) Teller Mission, AK (KTS) Telluride, CO (TEX) Tenakee Springs, AK (TKE) Terre Haute, IN (HUF) Tetlin, AK (TEH) Texarkana, AR (TXK) Thief River Falls, MN (TVF) Thorne Bay, AK (KTB) Tin City, AK (TNC) Togiak Village, AK (TOG) Tok, AK (TKJ) Toksook Bay, AK (OOK) Toledo, OH (TOL) Topeka, KS (FOE) Traverse City, MI (TVC) Trenton/Mercer, NJ (TTN) Tucson, AZ (TUS) Tulsa, OK (TUL) Tuluksak, AK (TLT) Tuntutuliak, AK (WTL) Tununak, AK (TNK) Tupelo, MS (TUP) Tuscaloosa, AL (TCL) Twin Falls, ID (TWF) Twin Hills, AK (TWA) Tyler, TX (TYR) Unalakleet, AK (UNK) Urbana/Champaign, IL (CMI) Utica, NY (UCA) Utopia Creek, AK (UTO) Vail, CO - Eagle County Airport (EGE) Vail, CO - Van service (QBF) Valdez, AK (VDZ) Valdosta, GA (VLD) Valparaiso, FL (VPS) Venetie, AK (VEE) Ventura/Oxnard, CA (OXR) Vernal, UT (VEL) Victoria, TX (VCT) Visalia, CA (VIS) Waco, TX (ACT) Wainwright, AK (AIN) Wales, AK (WAA) Walla Walla, WA (ALW) Washington DC - All airports (WAS) Washington DC - Dulles (IAD) Washington DC - National (DCA) Waterfall, AK (KWF) Waterloo, IA (ALO) Watertown, NY (ART) Watertown, SD (ATY) Wausau/Stevens Point, WI (CWA) Wenatchee, WA (EAT) West Palm Beach, FL (PBI) West Yellowstone, MT (WYS) Westchester County, NY (HPN) Westerly, RI (WST) Westsound, WA (WSX) Whale Pass, AK (WWP) White Mountain, AK (WMO) White River, VT (LEB) Wichita Falls, TX (SPS) Wichita, KS (ICT) Wilkes Barre, PA (AVP) Williamsburg, VA (PHF) Williamsport, PA (IPT) Williston, ND (ISN) Wilmington, NC (ILM) Windsor Locks, CT (BDL) Worcester, MA (ORH) Worland, WY (WRL) Wrangell, AK (WRG) Yakima, WA (YKM) Yakutat, AK (YAK) Yellowstone/Cody, WY (COD) Youngstown, OH (YNG) Yuma, AZ (YUM)
January 21, 2010
by Snippets Manager
· 4,494 Views
article thumbnail
GWT Development with IntelliJ IDEA Community Edition
I'll use IntelliJ IDEA Community Edition and GWT 2.0.
January 21, 2010
by Victor Tsoukanov
· 45,694 Views
article thumbnail
Struts 2 Tutorial: Struts 2 Ajax Tutorial with Example
Welcome to the last part of 7 article series of Struts 2 Framework tutorials. In we saw how to implement File Upload functionality in Struts 2. In this article we will see how we can implement Ajax support in a webapplication using Struts2 framework. Struts 2 Tutorial List Part 7: Struts 2 Ajax Tutorial with Example AJAX support in Struts 2 Struts 2 provides built-in support to AJAX using Dojo Toolkit library. If you are new to Dojo, you may want to go through the Introduction of DOJO Toolkit. Struts 2 comes with powerful set of Dojo AJAX APIs which you can use to add Ajax support. In order to add Ajax support, you need to add following JAR file in your classpath: struts2-dojo-plugin.jar Also once we add this JAR file, we need to add following code snippet in whatever JSP file we need to add AJAX support. First define the taglib sx which we will use to add AJAX enabled tags. Add this head tag in your JSP between … tags. This sx:head tag will include required javascript and css files to implement Ajax. AJAX Example: Struts2 Ajax Drop Down Let us add simple AJAX support in our StrutsHelloWorld web application. We will use the base code that we used in previous articles and add Ajax on top of it. We will create a drop down which will Autocomplete and suggest the input. For this we will add Dojo support to our webapp. Step 1: Adding JAR file As discussed earlier we will add struts2-dojo-plugin.jar in classpath (WEB-INF/lib). Thus, following is the list of required jar files. Note that these jars are needed to run full application including all the samples of previous parts of this tutorial series. Step 2: Create AJAX Action class We will create an action class which will get called for our Ajax example. Create a file AjaxAutocomplete.java in net.viralpatel.struts2 package and copy following content into it. AjaxAutocomplete.java package net.viralpatel.struts2; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import com.opensymphony.xwork2.ActionSupport; public class AjaxAutocomplete extends ActionSupport { private String data = "Afghanistan, Zimbabwe, India, United States, Germany, China"; private List countries; private String country; public String execute() { countries = new ArrayList(); StringTokenizer st = new StringTokenizer(data, ","); while (st.hasMoreTokens()) { countries.add(st.nextToken().trim()); } return SUCCESS; } public String getCountry() { return this.country; } public List getCountries() { return countries; } public void setCountries(List countries) { this.countries = countries; } public void setCountry(String country) { this.country = country; } } In above code we have created a simple action class with attribute String country and List countries. The countries list will be populated with country names when execute() method is called. Here for this example, we have loaded static data. You may feel free to change this and add data from database. Step 3: Create JSP Create JSP file to display Autocomplete textbox for our Ajax action. Create AjaxDemo.jsp in WebContent directory. AjaxDemo.jsp Struts 2 Autocomplete (Drop down) Example! Country: In above JSP file we have used sx:autocompleter tag to render an autocomplete drop down which users Ajax class to fetch data internally. Note that we have mapped the list attribute with List countries. Step 4: Creating Struts.xml entry Add following action entry in Struts.xml file: /ajaxdemo.tiles /ajaxdemo.tiles Notice that we are using Tiles here in this example. You may want to use AjaxDemo.jsp instead of /ajaxdemo.tiles to render the output directly in JSP. That’s All Folks Compile and Run the application in eclipse. Download Source Code Click here to download Source Code without JAR files (24KB) Conclusion Struts2 Framework provides wide variety of features to create a rich web application. In this Struts2 series we saw different aspects of Struts 2 like introduction of struts2, hello world application, validation framework, tiles plugin, strurts2 interceptors, file upload and ajax support.
January 20, 2010
by Viral Patel
· 124,982 Views
article thumbnail
Struts 2 Tutorial: Create Struts 2 Application in Eclipse
Welcome to the Part 2 of 7-part series where we will explore the world of Struts 2 Framework. In we went through the basics of Struts2, its Architecture diagram, the request processing lifecycle and a brief comparison of Struts1 and Struts2. If you have not gone through the previous article, I highly recommend you to do that before starting hands-on today. Struts 2 Tutorial List Part 7: Struts 2 Ajax Tutorial with Example Related: Create Struts Application with Eclipse Things We Need Before we starts with our first Hello World Struts 2 Example, we will need few tools. JDK 1.5 above (download) Tomcat 5.x above or any other container (Glassfish, JBoss, Websphere, Weblogic etc) (download) Eclipse 3.2.x above (download) Apache Struts2 JAR files:(download). Following are the list of JAR files required for this application. commons-logging-1.0.4.jar freemarker-2.3.8.jar ognl-2.6.11.jar struts2-core-2.0.12.jar xwork-2.0.6.jar Note that depending on the current version of Struts2, the version number of above jar files may change. Our Goal Our goal is to create a basic Struts2 application with a Login page. User will enter login credential and if authenticated successfully she will be redirected to a Welcome page which will display message ”Howdy, …!“. If user is not authenticated, she will be redirected back to the login page. Getting Started Let us start with our first Struts2 based application. Open Eclipse and goto File -> New -> Project and select Dynamic Web Project in the New Project wizard screen. After selecting Dynamic Web Project, press Next. Write the name of the project. For example StrutsHelloWorld. Once this is done, select the target runtime environment (e.g. Apache Tomcat v6.0). This is to run the project inside Eclipse environment. After this press Finish. Once the project is created, you can see its structure in Project Explorer. Now copy all the required JAR files in WebContent -> WEB-INF -> lib folder. Create this folder if it does not exists. Mapping Struts2 in WEB.xml As discussed in the previous article (Introduction to Struts2), the entry point of Struts2 application will be the Filter define in deployment descriptor (web.xml). Hence we will define an entry of org.apache.struts2.dispatcher.FilterDispatcher class in web.xml. Open web.xml file which is under WEB-INF folder and copy paste following code. Struts2 Applicationstruts2org.apache.struts2.dispatcher.FilterDispatcherstruts2/*Login.jsp The above code in web.xml will map Struts2 filter with url /*. The default url mapping for struts2 application will be /*.action. Also note that we have define Login.jsp as welcome file. The Action Class We will need an Action class that will authenticate our user and holds the value for username and password. For this we will create a package net.viralpatel.struts2 in the source folder. This package will contain the action file. Create a class called LoginAction in net.viralpatel.struts2 package with following content. package net.viralpatel.struts2;public class LoginAction {private String username;private String password;public String execute() {if (this.username.equals("admin")&& this.password.equals("admin123")) {return "success";} else {return "error";}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;} Note that, above action class contains two fields, username and password which will hold the values from form and also contains an execute() method that will authenticate the user. In this simple example, we are checking if username is admin and password is admin123. Also note that unlike Action class in Struts1, Struts2 action class is a simple POJO class with required attributes and method. The execute() method returns a String value which will determine the result page. Also, in Struts2 the name of the method is not fixed. In this example we have define method execute(). You may want to define a method authenticate() instead. The ResourceBundle ResourceBundle is very useful Java entity that helps in putting the static content away from the source file. Most of the application define a resource bundle file such as ApplicationResources.properties file which contains static messages such as Username or Password and include this with the application. ResourceBundle comes handy when we want to add Internationalization (I18N) support to an application. We will define an ApplicationResources.properties file for our application. This property file should be present in WEB-INF/classes folders when the source is compiled. Thus we will create a source folder called resources and put the ApplicationResources.properties file in it. To create a source folder, right click on your project in Project Explorer and select New -> Source Folder. Specify folder name resources and press Finish. Create a file ApplicationResources.properties under resources folder. Copy following content in ApplicationResources.properties. label.username= Usernamelabel.password= Passwordlabel.login= Login The JSP We will create two JSP files to render the output to user. Login.jsp will be the starting point of our application which will contain a simple login form with username and password. On successful authentication, user will be redirected to Welcome.jsp which will display a simple welcome message. Create two JSP files Login.jsp and Welcome.jsp in WebContent folder of your project. Copy following content into it. Login.jsp Struts 2 - Login Application Welcome.jsp Howdy, ...! Note that we have used struts2 tag to render the textboxes and labels. Struts2 comes with a powerful built-in tag library to render UI elements more efficiently. The struts.xml file Struts2 reads the configuration and class definition from an xml file called struts.xml. This file is loaded from the classpath of the project. We will define struts.xml file in the resources folder. Create file struts.xml in resources folder. Copy following content into struts.xml. Welcome.jspLogin.jsp Note that in above configuration file, we have defined Login action of our application. Two result paths are mapped with LoginAction depending on the outcome of execute() method. If execute() method returns success, user will be redirected to Welcome.jsp else to Login.jsp. Also note that a constant is specified with name struts.custom.i18n.resources. This constant specify the resource bundle file that we created in above steps. We just have to specify name of resource bundle file without extension (ApplicationResources without .properties). Our LoginAction contains the method execute() which is the default method getting called by Sturts2. If the name of method is different, e.g. authenticate(); then we should specify the method name in tag. Almost Done We are almost done with the application. You may want to run the application now and see the result yourself. I assume you have already configured Tomcat in eclipse. All you need to do: Open Server view from Windows -> Show View -> Server. Right click in this view and select New -> Server and add your server details. To run the project, right click on Project name from Project Explorer and select Run as -> Run on Server (Shortcut: Alt+Shift+X, R) But there is one small problem. Our application runs perfectly fine at this point. But when user enters wrong credential, she is redirected to Login page. But no error message is displayed. User does not know what just happened. A good application always show proper error messages to user. So we must display an error message Invalid Username/Password. Please try again when user authentication is failed. Final Touch To add this functionality first we will add the error message in our ResourceBundle file. Open ApplicationResources.properties and add an entry for error.login in it. The final ApplicationResources.properties will look like: label.username= Usernamelabel.password= Passwordlabel.login= Loginerror.login= Invalid Username/Password. Please try again. Also we need to add logic in LoginAction to add error message if user is not authenticated. But there is one problem. Our error message is specified in ApplicationResources.properties file. We must specify key error.login in LoginAction and the message should be displayed on JSP page. For this we must implement com.opensymphony.xwork2.TextProvider interface which provides method getText(). This method returns String value from resource bundle file. We just have to pass the key value as argument to getText() method. The TextProvider interface defines several method that we must implement in order to get hold on getText() method. But we don’t want to spoil our code by adding all those methods which we do not intend to use. There is a good way of dealing with this problem. Struts2 comes with a very useful class com.opensymphony.xwork2.ActionSupport. We just have to extend our LoginAction class with this class and directly use methods such as getText(), addActionErrors() etc. Thus we will extend the LoginAction class with ActionSupport class and add the logic for error reporting into it. The final code in LoginAction must look like: package net.viralpatel.struts2;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport {private String username;private String password;public String execute() {if (this.username.equals("admin")&& this.password.equals("admin123")) {return "success";} else {addActionError(getText("error.login"));return "error";}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;} And that’s it. Our first Hello World Struts2 Application is now ready. That’s All Folks Execute the application in Eclipse and run it in your favorite browser. Login page Welcome page Login page with error Download Source Code Click here to download Source Code without JAR files (9KB). Moving On Now that we have created our first webapp using Struts2 framework, we know how the request flows in Struts2. We also know the use of struts.xml and properties file. In this application we implemented a preliminary form of validation. In we will learn more about Validation Framework in Struts2 and implement it in our example. Original article: http://viralpatel.net/blogs/2009/12/tutorial-create-struts-2-application-eclipse-example.html
January 15, 2010
by Viral Patel
· 290,067 Views
  • Previous
  • ...
  • 892
  • 893
  • 894
  • 895
  • 896
  • 897
  • 898
  • 899
  • 900
  • 901
  • ...
  • Next
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook
×