background preloader

Desarrollo Java

Facebook Twitter

Informacion relativa a cualquier tema relacionado con el mundo del desarrollo Java

Explore the Dynamic Proxy API. With the introduction of the Dynamic Proxy API in Java 1.3, a huge and often overlooked improvement has been made to the Java platform. The uses for dynamic proxies are sometimes hard concepts to grasp. In this article, I hope to introduce you first to the Proxy design pattern and then to the java.lang.reflect.Proxy class and the java.lang.reflect.InvocationHandler interface, which make up the heart of Dynamic Proxy's functionality. The functionality discussed here combines the Java 1.3 dynamic proxies with abstract data types to bring strong typing to those types. I will also discuss the power of the dynamic proxy by introducing the concept of views in your Java programming. Lastly, I will introduce a powerful way to add access control to your Java objects with, of course, the use of the dynamic proxy. Definition of a proxy A proxy forces object method calls to occur indirectly through the proxy object, which acts as a surrogate or delegate for the underlying object being proxied.

. , or . GlassFish JDBC Security with Salted Passwords on MySQL. One of the most successful posts on this blog is my post about setting up a JDBC Security Realm with form based authentication on GlassFish. Some comments on this post made me realize that there is more to do to actually make this secure as it should be. Security out of the box GlassFish comes with a GlassFish JDBC Realm already. All you have to do is to initialize a database and get the security configuration right and you are done.

Among the standard configuration you have the option to define a digest-algorithm (including encoding and charset). What is weak or missing? The out of the box solution goes a very trivial way. Adding some Salt The above approaches work because of the fact that each password is hashed in the exact same way. Preparations We are clearly leaving the standard JDBCRealm features now. Now we implement the basic realm. But the most important part is missing here. Setting up some tests I’m not exactly the kind of test driven guy but in this case it actually makes sense.

Schema Creation Script With Hibernate 4, JPA And Maven. The scenario is trivial – you want to generate a database schema creation script while building your application (and then execute the script on the target database) This was relatively easy with Hibernate 3, as there was the hibernate3-maven-plugin, but it is not compatible with Hibernate 4. And for every new project you should start with Hibernate 4, of course.

So what to do? It’s relatively simple, but takes some time to research and test. The idea is to use the SchemaExport tool. But it’s a bit tricky, because it only supports native Hibernate configuration and not JPA. First, you create a command-line application that handles the export. Note that Ejb3Configuration is deprecated, but it is deprecated for external use – hibernate are using it internally quite a lot. Note here, that we are not directly deploying the file to the target database. Then we have to just invoke this class from the maven build. Given email address is already subscribed, thank you! Oops. Logback: Logging revisited.

Hi, I am back again with my rant about logging as an inherent part of any application design and development. I am a big fan of strong basics, and in my humble opinion logging is one of those often overlooked but basic critical element of any enterprise grade application. I have written about this before here.

This is not really mandatory to read in order to make sense of the current article, but it might help to give it a cursory look, to set the context for this article. In the first article, I introduced logging as a high benefit, low cost alternative to the omnipresent System.out.println(), that all java folks love so much. It is certainly not new kid in the block – and that is one of the reasons I am suggesting you consider this for enterprise grade applications to start with.

Being around is one thing and making your mark is different. The very first thing that Logback promises is faster implementation (at this link). I start by creating a vanilla java application using Maven. How to change logging level in runtime. Changing the log logging level in runtime is important mainly in production environment where you might want to have debug logging for limited amount of time.

Well, changing the root logger is very simple – assuming you have an input parameter with the wanted logging level simply get the root logger and set by the input logging level, such as: However – the common case is that we maintain log instance per class, for example: and setting the root logger is not enough, since the class level logger will not be affected. The trick is to remember get all the loggers in the system and change their logging level too. For example: So just wrap it up in a service class and call it from your controller, with a dynamic logLevel String parameter which represent the logging level you wish to set your system to.

If any of you need the complete solution, please let me know. The basic approach is in this link. Given email address is already subscribed, thank you! Oops. Please provide a valid email address. Java: Mergesort using Fork/Join Framework. The objective of this entry is to show a simple example of a Fork/Join RecursiveAction, not to delve too much into the possible optimizations to merge sort or the relative advantages of using Fork/Join Pool over the existing Java 6 based implementations like ExecutorService. The following is a typical implementation of a Top Down Merge sort algorithm using Java: To quickly describe the algorithm – The following steps are performed recursively: The input data is divided into 2 halves Each half is sortedThe sorted data is then merged Merge sort is a canonical example for an implementation using Java Fork/Join pool, and the following is a blind implementation of Merge sort using the Fork/Join framework: The recursive task in Merge sort can be succinctly expressed as an implementation of RecursiveAction – Given this the only work left is to use a ForkJoinPool to submit this task.

This is how the task of submitting the task to the Fork/Join Pool looks like: Oops. Please complete the CAPTCHA. DAO layer – Generics to the rescue. Generics can be a powerful tool to create reusable code with the power of compile time verification (type safety..). Unfortunately I feel the main stream developers still afraid of it. However, in analogy to Hagrid’s spiders I would say that Generics are seriously misunderstood creatures… :-) I hope the following example will demonstrate how useful they can be.

The Problem - DAO (Data Access Objects) classes have common methods such as save, update, delete, loadAll.. which are required in every DAO class. Writing a base class with these common methods and making every DAO object extend it, is simply not enough since each DAO class represents a different domain class and therefore the type used in the common methods’ signature is different (although implementation is similar), for example: How Generics can help us create a base class with a common implementation and yet, keep method signature type-safety? First, we need to define an interface with the common methods: And that’s it !

1. 2. Oops. Java Executor Service Types. ExecutorService feature was come with Java 5. It extends Executor interface and provides thread pool feature to execute asynchronous short tasks. There are five ways to execute the tasks asyncronously by using ExecutorService interface provided Java 6. ExecutorService execService = Executors.newCachedThreadPool(); This method of the approach creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. If no existing thread is available, a new thread will be created and added to the pool. ExecutorService execService = Executors.newFixedThreadPool(10); This method of the approach creates a thread pool that reuses a fixed number of threads. ExecutorService execService = Executors.newSingleThreadExecutor(); Methods of the ExecutorService : execute(Runnable) : Executes the given command at some time in the future.

Java Code Example for. Configuring Maven to use SLF4J. I mainly write articles for two reasons: to understand something that is new to me or to document something I regularly have to explain to others. This article definitely falls in the second category: in order to celebrate the new 1.0.0 version of Logback, I’ve decided to write down once and for all how to properly use SLF4J with Maven since it seems there’s no shortage of questions about it. Basics The basis of SLF4J is to have two separate components, one API and one implementation. That means that your code should solely be dependent on the API thus the implementation can be changed at your convenience.

Remember that decoupling code from implementations by introducing an interface should be your first concern: at the library level, it’s the role of an API. This is easily done like this in your POM: The runtime scope prevents us from using Logback’s implementation classes when coding, which achieves decoupling. Bridging Like in the section above, you should preferably use the To go further: Java Annotations. Java annotations are used to provide meta data for your Java code.

Being meta data, the annotations do not directly affect the execution of your code, although some types of annotations can actually be used for that purpose. Java annotations were added to Java from Java 5. This text covers Java annotations as they look in Java 6. As far as I know, Java annotations have not changed in Java 7, so this text should be valid for Java 7 programmers too.

Java Annotation Purposes Java annotations are typically used for the following purposes: Compiler instructions Build-time instructions Runtime instructions Java has 3 built-in annotations that you can use to give instructions to the Java compiler. Java annotations can be be used at build-time, when you build your software project. Normally, Java annotations are not present in your Java code after compilation. Annotation Basics An annotation in its shortest form looks like this: @Entity Annotation Elements @Entity(tableName = "vehicles") @Deprecated.

Librerias

Java Proxies. MySQL Stored Procedure Tutorial. Each tutorial is packed with the easy-to-understand examples with detailed explanations. If you go through all the tutorials, you can develop the simple to complex stored procedures in MySQL. Introduction to MySQL Stored Procedures This tutorial introduces to you MySQL stored procedures, their advantages and disadvantages. Getting Started with MySQL Stored Procedures In this tutorial, we will show you step by step how to develop the first MySQL stored procedure by using the CREATE PROCEDURE statement. In addition, we will show you how to call the stored procedure from SQL statements. MySQL Stored Procedure Variables You will learn about variables in MySQL stored procedure, how to declare and use variables. MySQL Stored Procedure Parameters This tutorial shows you how to define parameters in stored procedure and introduces you to different parameter modes including IN, OUT and INOUT. MySQL Stored Procedures That Return Multiple Values MySQL IF Statement MySQL CASE Statement MySQL Cursor.

Task management with Eclipse Mylyn. Eclipse Mylyn is a software component packaged with the Eclipse IDE. The idea of Mylyn is to integrate tasks into the Eclipse IDE and connect the current state of the Eclipse IDE, e.g. the context with such a task. Software developers perform a variety of activities. They write source code for new functionality, fix bugs, write documentation, answer questions, attend meetings and much more. Developing software in the Eclipse IDE involves writing new classes or methods and modifying existing code. For example the procedure of fixing a bug may involve writing unit tests, refactoring existing code, reading and searching involved code and finally fixing the affected code.

Eclipse Mylyn allows the developer to record his activities in such a task while he is working on it. When switching between tasks, the corresponding context is restored and the editors which belong to this tasks are opened and the others are closed. Mylyn can also filter information in the IDE. 1.2. 1.3. Http:

Testing

Patrones de diseño. Tratando de entenderlo: Manejo de ramas en Subversion desde Eclipse. En este artículo vamos a ver cómo trabajar desde Eclipse con las ramas de desarrollo de un proyecto gestionado en Subversion. Lo primero que vamos a hacer es crear una rama del proyecto, en la cual podríamos desarrollar una nueva característica del proyecto o corregir un error. Para crear esta rama hacemos clic sobre el proyecto con el botón derecho del ratón y vamos a Team -> Branch/Tag: Para Subversion es lo mismo una rama (branch) que una etiqueta (tag).

Una estiqueta es una rama sobre la que no se incluyen cambios. A continuación seleccionamos el directorio remoto donde se almacenará esta rama. Por convencion, todas las ramas se guardan bajo el directorio branches del repositorio: Seleccionamos la revisión sobre la que se va a crear la rama. Por último podemos incluir un comentario e indicar que queremos que nuestra copia local del proyecto se cambie a esta rama para poder desarrollar sobre ella: Seleccionamos el tipo de merge que vamos a realizar.

Java Standard

Spring.