My favorite features of Scala – part I

During the last few months I’ve spent substantial amount of free time learning Scala and consider it time well spent. This article is first part describing my favorite features of Scala

Extractor Objects

Whenever you define a val or var, what comes after the keyword is not simply an identifier but rather a pattern.

val regex = "(\\d+)/(\\d+)/(\\d+)".r
val regex(year, month, day) = "2015/7/29"

The val regex is an instance of Regex, and when you use it in a pattern, you’re implicitly calling regex.unapplySeq , which extracts the match groups into a Seq[String], the elements of which are assigned in order to the variables year, month, and day.

Detecting missing and unnecessarily imported packages in OSGi bundles

When developing OSGi based application in Eclipse adding required import package declaration is assisted by PDE. In practice, it means that import package declarations are never missing – otherwise Eclipse project won’t comile and run. What programmers almost never do is to remove unnecessary declarations. Why would they if it works? In practice bundle manifests polluted with unnecessary imported packages can cause performance issues during bundle resolution and classloading and sometimes confuse Eclipse PDE during development.

To mitigate this problem I’ve created a small application leveraging the power of excellent BND library. I was really surprised when I run this tool against bundles from application I’m currently working on – some had literally hundreds of unnecessarily imported packages.

oipv 1.x
Usage: oipv [options]

  -j <jar1>,<jar2>... | --jars <jar1>,<jar2>...
        jars or folders to include
  -e <package1>,<package2> | --exclude-package <package1>,<package2>
        Package prefixes to exclude from analysis. You may want to
        excluded packages exported by JRE - javax.swing, javax.sql, etc.
        to avoid false positives
  --help
        print usage text

Application code available at GitHub (github.com) – https://github.com/jarek-przygodzki/osgi-import-package-verifier. The app itself is available here: oipv-1.0-master-f6c5c79.zip

Delete all disabled branches of a Bamboo build plan

Recently I found myself doing some Bamboo maintenance. Namely, I had to delete all disabled branches of build plan related to already deleted feature branches that Bamboo failed to automatically remove. At first, I tried this shell script, but it didn’t work for me with Bamboo 5.6.2 and I ended writing my own version. I’m posting it here hoping it will be useful for others too

Usage is very simple

groovy bamboo_delete_disabled_branches.groovy \
      --bamboo-url <BAMBOO_URL, i.e. http://mycompany/bamboo> \
      --plan-key <PLAN_KEY> \
      --user <USER> --password <PASSWORD>

You can also perform a simulation without actually deleting anything with „–dry-run” option.

Sprawdzanie wersji plików JAR

Niedawno znalazłem się w sytuacji, gdy musiałem sprawdzić wersje plików JAR by zapewnić zgodność tworzonego release-a z Javą 6. Nie udało mi się znaleźć gotowego narzędzia, więc napisałem coś własnego: github.com/jarek-przygodzki/jarversion.

Program jest napisany w Scali, ale dla użytkownika nie ma to znaczenia. Po zbudowanie projektu użycia jest bardzo proste

$ jarversion

jarversion 1.x
Usage: jarversion [options]

  -j <jar1>,<jar2>... | --jars <jar1>,<jar2>...
        jars to include
  --verbose
        be verbose
  --help
        print usage text
Print jar file version number defined as the maximum version of all classes contained within jar file

$ jarversion --jars  "/apps/dev/eclipse-jee-mars-R/eclipse/plugins/com.ibm.icu_54.1.1.v201501272100.jar,/apps/dev/eclipse-jee-mars-R/eclipse/plugins/com.sun.el_2.2.0.v201303151357.jar"

/apps/dev/eclipse-jee-mars-R/eclipse/plugins/com.ibm.icu_54.1.1.v201501272100.jar = ClassVersion(49,0)
/apps/dev/eclipse-jee-mars-R/eclipse/plugins/com.sun.el_2.2.0.v201303151357.jar = ClassVersion(50,0)

Można oczywiście poprawić kilka rzeczy, w szczególności format wyjścia i prezentować informacje, że np. 45.0 to JDK 1.1 a 52.0 to J2SE 8 ale nawet w obecnej postaci aplikacja spełnia swoje zadanie

Bundle-NativeCode: Using platform-specific DLLs from OSGi

Originally posted on holistic tendencies:

I’m currently doing some development on a large RCP application. One recent task required using a Windows DLL to open an email message through MAPI, which is made pretty easy with OSGi. But I encountered a few issues that I’m sure will bite other people.

Bundle-NativeCode

OSGi now provides some support for resolving and loading shared libraries/DLLs in Java applications. A bundle declaratively specifies its DLLs and associated platform requirements in their bundle manifest using the Bundle-NativeCode header. This header value is then used when loading a DLL through System.loadLibrary().

TheBundle-NativeCode header specifies a number of comma-delimited clauses. For example:

Bundle-NativeCode: lib/win32/x86/http.dll; lib/win/zlib.dll;
    processor=x86; osname=win32, 
  lib/macosx/libhttp.dylib; lib/macosx/libzlib.jnilib;
    osname=macosx; processor=x86; processor=x86_64,
  *

A clause specifies one or more DLLs. A clause is matched to the current platform using a set of parameters (e.g., processor and osname, but also osversion, language, or a selection-filter). Parameters of…

View original 408 słów więcej

Arquillian – solving org.dbunit.database.AmbiguousTableNameException: OL$

Jeśli podczas uruchomienia testów integracyjnych wykorzystujących Arquillian Persistence Extension uruchmionych względem bazy Oracle pojawi się błąd

arquillian.persistence.dbunit.exception.DBUnitDataSetHandlingException: Unable to clean database.
	...
Caused by: org.dbunit.database.AmbiguousTableNameException: OL$
	at org.dbunit.dataset.OrderedTableNameMap.add(OrderedTableNameMap.java:198)
	at org.dbunit.database.DatabaseDataSet.initialize(DatabaseDataSet.java:231)
	at org.dbunit.database.DatabaseDataSet.getTableNames(DatabaseDataSet.java:272)
	at org.dbunit.database.DatabaseDataSet.createIterator(DatabaseDataSet.java:258)
	at org.dbunit.dataset.AbstractDataSet.iterator(AbstractDataSet.java:189)
	at org.dbunit.dataset.filter.AbstractTableFilter.iterator(AbstractTableFilter.java:91)
	at org.dbunit.dataset.FilteredDataSet.createIterator(FilteredDataSet.java:89)
	at org.dbunit.dataset.AbstractDataSet.iterator(AbstractDataSet.java:189)
	at org.dbunit.operation.DeleteAllOperation.execute(DeleteAllOperation.java:92)
	at org.jboss.arquillian.persistence.dbunit.cleanup.StrictCleanupStrategyExecutor.cleanupDatabase(StrictCleanupStrategyExecutor.java:56)
	... 181 more

to rozwiązaniem jest dodanie do pliku arquillian.xml wpisu

<extension qualifier="persistence-dbunit">
	<property name="schema">$nazwa-schematu$</property>
</extension>

Precedence: ordering or grouping?

Originally posted on Jon Skeet's coding blog:

As I’ve mentioned before, I’m part of the technical group looking at updating the ECMA-334 C# standard to reflect the C# 5 Microsoft specification. I recently made a suggestion that I thought would be uncontroversial, but which caused some discussion – and prompted this „request for comment” post, effectively.

What does the standard say about precedence?

The current proposed standard includes the following text:

The order of evaluation of operators in an expression is determined by the precedence and associativity of the operators (§13.4.2).

Operands in an expression are evaluated from left to right.

When an expression contains multiple operators, the precedence of the operators controls the order in which the individual operators are evaluated. [Note: For example, the expression x + y * z is evaluated as x + (y * z) because the * operator has higher precedence than the binary + operator. end note

View original 857 słów więcej