GEDOPLAN

Java EE mit Java 8 verschönern!

AlleCDIJPAJSF

J2EE war recht komplex und unübersichtlich, was viele durchaus zu Recht zu anderen Plattformen getrieben hat. Seit Java EE 5/6/7 ist das Thema aber deutlich verbessert: Der Fokus liegt nun eindeutig auf der Unterstützung der Entwickler und nicht mehr, wie zuvor, auf der für den Betrieb notwendigen Technik.

Durch Java 8 läßt sich nun noch eine weitere Verschönerung erreichen. Damit werden Java-EE-Anwendungen deutlich strukturierter!

Das folgende kleine Java-8-Programm tut den Trick:

package de.gedoplan.beantrial;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;

public class SourceCleaner
{

  public static void main(String[] args) throws IOException
  {
    for (String filename : args)
    {
      Files
        .lines(Paths.get(filename))
        .flatMapToInt(line -> line.codePoints())
        .mapToObj(i -> Character.valueOf((char) i))
        .collect(Collectors.groupingBy(x -> x, Collectors.summingInt(x -> 1)))
        .entrySet()
        .stream()
        .map(e -> new String(new char[e.getValue()]).replace('\0', e.getKey()))
        .forEach(System.out::println);
    }
  }
}

Aus einer unübersichtlichen Quelle wie der folgenden

package de.gedoplan.seminar.cdi.demo.intercept.interceptor;

import java.io.Serializable;

import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import javax.transaction.Status;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

import org.apache.commons.logging.Log;

/**
 * Interceptor-Implementierung zu {@link TransactionRequired}.
 * 
 * @author dw
 */
@TransactionRequired
@Interceptor
public class TransactionRequiredInterceptor implements Serializable
{
  private static final long serialVersionUID = 1L;

  @Inject
  Log                       logger;

  @Inject
  UserTransaction           userTransaction;

  /**
   * Interceptor-Arbeitsmethode.
   * 
   * @param invocationContext InvocationContext
   * @return Returnwert
   * @throws Exception bei Fehlern
   */
  @AroundInvoke
  public Object manageTransaction(InvocationContext invocationContext) throws Exception
  {
    // Falls schon eine TX aktiv, Methode direkt aufrufen
    if (isTransactionActive())
    {
      return invocationContext.proceed();
    }

    // TX beginnen
    if (this.logger.isDebugEnabled())
    {
      this.logger.debug("Begin tx");
    }
    this.userTransaction.begin();

    try
    {
      // Methode aufrufen
      Object result = invocationContext.proceed();

      // TX committen
      if (this.logger.isDebugEnabled())
      {
        this.logger.debug("Commit tx");
      }
      this.userTransaction.commit();

      return result;
    }
    catch (Exception e) // CHECKSTYLE:IGNORE Allgemeine Exception ist hier OK
    {
      // TX zurückrollen
      if (this.logger.isDebugEnabled())
      {
        this.logger.debug("Rollback tx");
      }
      try
      {
        this.userTransaction.rollback();
      }
      catch (Throwable ignore)
      {
      }

      throw e;
    }

  }

  // Ist die TX aktiv?
  private boolean isTransactionActive() throws SystemException
  {
    return this.userTransaction.getStatus() == Status.STATUS_ACTIVE;
  }
}

wird die folgende, übersichliche und auch kürzere Form:

                                                                                                                                                                                                                                                                                                                                                                                                        
""""""
(((((((((((((((((((((
)))))))))))))))))))))
**************
,
--
...................................................
//////////////////
1
:
;;;;;;;;;;;;;;;;;;;;;;;;
====
?
@@@@@@@@@@
AAAAAAAA
B
CCCCCCCCCCC
DDDD
EEEEEEEEEEEEE
FF
G
H
IIIIIIIIIIIIIIIIII
KK
LLLL
MM
N
OOOO
RRRRRR
SSSSSSSSSS
TTTTTTTTTTTTTTTTTTTTTTT
UUUU
VV
XXXXX
Y
_
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbb
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
ddddddddddddddddddddddd
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
fffffffff
ggggggggggggggggggggggggggggggggggggg
hhhhhhhhhhhhhhhhhhhhhhhhh
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
jjjjjjjjjjjjjj
kkkkkkkkkk
llllllllllllllllllllllllllllllllllllllll
mmmmmmmmmmmmmmmmmmmmmmmmmmmmm
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
ppppppppppppppppppppppppppppppppppppp
qqq
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt
uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
vvvvvvvvvvvvvvvvvvvvvvv
wwwwwww
xxxxxxxxxxxxxxxxxxxxxxx
yyyy
zzzz
{{{{{{{{{{{{
ü
}}}}}}}}}}}}

Probieren Sie’s aus. Es funktioniert übrigens nicht nur für Java-Quellen, sondern auch für JavaScript, HTML, ja sogar für C#!

Autor

Diesen Artikel teilen

LinkedIn
Xing

Gibt es noch Fragen?

Fragen beantworten wir sehr gerne! Schreibe uns einfach per Kontaktformular.

Schulungen mit der selben Kategorie:

Blogkategorie: Alle
Es wurden keine Ergebnisse gefunden.

weitere Artikel

Kontakt

Brauchen Sie eine individuelle IT-Schulung, eine fundierte Beratung oder eine individuelle Softwareentwicklung? Dann sind Sie hier genau richtig!

Tim Neumann

Geschäftsleitung

GEDOPLAN GmbH
Stieghorster Straße 60
33605 Bielefeld

GEDOPLAN GmbH
Kantstraße 164
10623 Berlin

    Kontakt

    Tim Neumann

    Geschäftsleitung

    GEDOPLAN GmbH
    Stieghorster Straße 60
    33605 Bielefeld

    GEDOPLAN GmbH
    Kantstraße 164
    10623 Berlin

    Brauchen Sie eine individuelle IT-Schulung, eine fundierte Beratung oder eine individuelle Softwareentwicklung? Dann sind Sie hier genau richtig!