Categories
discuss

MVN compile not using UTF-8 encoding

Ok, this is a weird problem: I have a java test file that uses some UTF-8 characters. When I compile it with Maven, using

mvn -Dfile.encoding=UTF-8 -Dproject.build.sourceEncoding=UTF-8 test

(thus setting both the perceived platform encoding and the source file encoding, see maven platform encoding) I get something like

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building project
[INFO]    task-segment: [test]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory path/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources {execution: default-testResources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory path/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Compiling 7 source files to path/target/test-classes
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Compilation failure
path/to/file.java:[42,23] unclosed character literal
path/to/file.java:[42,25] ';' expected
path/to/file.java:[42,26] unclosed character literal
path/to/file.java:[47,23] unclosed character literal
path/to/file.java:[47,25] illegal character: 182
path/to/file.java:[47,26] unclosed character literal

When I compile the file with

javac path/to/file.java

I get similar errors:

path/to/file.java:42: unclosed character literal
    illegalCharEnc('ä');
                   ^
path/to/file.java:42: ';' expected
    illegalCharEnc('ä');
                     ^
path/to/file.java:42: unclosed character literal
    illegalCharEnc('ä');
                      ^
path/to/file.java:47: unclosed character literal
    illegalCharDec('ö');
                   ^
path/to/file.java:47: illegal character: 182
    illegalCharDec('ö');
                     ^
path/to/file.java:47: unclosed character literal
    illegalCharDec('ö');
                      ^
6 errors

Now when I use

javac -encoding UTF-8 path/to/file.java

instead, I get cannot find symbol errors, because of the missing dependencies. So I figure the problem is that javac is not called with UTF-8 option in Maven when compiling tests (notice how Using 'UTF-8' encoding to copy filtered resources. is missing in compiler:testCompile-section). Is this conclusion correct? Am I missing something? Is this a known problem? Anything I can do about it? Obviously, the platform encoding on my system is not UTF-8, but I currently cannot change that.

Answer

Having had the same problem I came across this question first. But as there was no answer I looked further and found another question that was answered and helped me solving this:

https://stackoverflow.com/a/10375505/332248 (credit to @chrisapotek and @Jopp Eggen for answering this)

Categories
discuss

load show_ads.js once

So we run google ads on our sites.

And I got thinking, each ad block ( of various sizes ) loads ..

<script type="text/javascript"
            src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

eg:

<script type="text/javascript"><!--
            google_ad_client = "ca-pub-xxxxxxxxxxxxxx";
            /* ad served */
            google_ad_slot = "xxxxxxxxx";
            google_ad_width = 728;
            google_ad_height = 90;
            //-->
            </script>
            <script type="text/javascript"
            src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
        </script>

and other ads, of different slot ( id’s ) amd sizes, on the same page also load :

http://pagead2.googlesyndication.com/pagead/show_ads.js

each time. So a page with say three ads on, loads that exact same js file 3 times ..

Removing that script file, adding it ONCE to the head, doesnt work for us.. the ads just dont show.

So is there a method of loading that show_ads.js file just once / page load ?

Answer

The Google ads script utilizes document.write(), which means the script must be in the position in the HTML that you want the ad.

However, fear not: the show_ads.js file will only be downloaded by your browser once. The subsequent <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js">s will load from cache.

In fact, the Cache-Control headers tell your browser it can load the file directly from cache for the next hour, so there should be only one trip to pagead2.googlesyndication.com per session, no matter how many pages are viewed.

Categories
discuss

Java AES CBC Decryption

PHP Encrypt Function

$privateKey = "1234567812345678";
$iv = "1234567812345678";
$data = "Test string";

$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $privateKey, $data, MCRYPT_MODE_CBC, $iv);

echo(base64_encode($encrypted));

Result: iz1qFlQJfs6Ycp+gcc2z4w==

When I try to decrypt this result in Java using the function below, all I get back is ì�š@ÔBKxnfÈ~¯Ô’M while I am expecting “Test string”. Any ideas where I am wrong? Thanks

public static String decrypt() throws Exception{
    try{
        String Base64EncodedText = "iz1qFlQJfs6Ycp+gcc2z4w==";
        String decodedText = com.sun.xml.internal.messaging.saaj.util.Base64.base64Decode(Base64EncodedText);
        String key = "1234567812345678";
        String iv = "1234567812345678";

        javax.crypto.spec.SecretKeySpec keyspec = new javax.crypto.spec.SecretKeySpec(key.getBytes(), "AES");
        javax.crypto.spec.IvParameterSpec ivspec = new javax.crypto.spec.IvParameterSpec(iv.getBytes());

        javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("AES/CBC/NoPadding");
        cipher.init(javax.crypto.Cipher.DECRYPT_MODE, keyspec, ivspec);
        byte[] decrypted = cipher.doFinal(decodedText.getBytes());

        String str = new String(decrypted);

        return str;

    }catch(Exception e){
        return null;
    }   
}

Answer

EDIT: As of Java 8 Java now includes an acceptable Base64 class, java.util.Base64.


This line

String decodedText = com.sun.xml.internal.messaging.saaj.util.Base64.base64Decode(Base64EncodedText);

looks wrong. Instead, use the apache commons codec classes or the Harder base64 class. Also the default padding used by mcrypt, zero padding, is arguably wrong and makes it difficult to use the results in other languages. The users comments section for the mcrypt_encrypt web pages has examples of how to do this.

Here is small example that uses the apache commons classes to decrypt your string.

import java.nio.charset.Charset;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

public class AESToy3 {

    private static final Charset ASCII = Charset.forName("US-ASCII");

    public static void main(String[] args) throws Exception {
        String base64Cipher = "iz1qFlQJfs6Ycp+gcc2z4w==";
        byte [] cipherBytes = Base64.decodeBase64(base64Cipher);
        byte [] iv = "1234567812345678".getBytes(ASCII);
        byte [] keyBytes = "1234567812345678".getBytes(ASCII);

        SecretKey aesKey = new SecretKeySpec(keyBytes, "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/NOPADDING");
        cipher.init(Cipher.DECRYPT_MODE, aesKey, new IvParameterSpec(iv));

        byte[] result = cipher.doFinal(cipherBytes);
        System.out.println(Hex.encodeHexString(result));
    }

}

this produces the following output:

5465737420737472696e670000000000

which when decoded as ASCII and removing the trailing zeros gives you Test string

Categories
discuss

How can I refresh my ListFragment when it returns to the layout from back stack?

First I should mention that I am using the ActionBarSherlock library for backwards compatibility.

I have an activity which adds a ListFragment when it is first started. I have a custom Loader which I implemented and follows the AsnycTaskLoader example very closely. My ListFragment implements the LoaderCallbacks<Cursor> interface. All the appropriate callback methods are called when the fragment is added (onCreateLoader() , onLoaderFinished() ) and when it is replaced (onLoaderReset() ).

My onActivityCreated(Bundle) method looks like this:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mAccountsDbAdapter = new AccountsDbAdapter(getActivity().getApplicationContext());

    setHasOptionsMenu(true);
    mCursorAdapter = new AccountsCursorAdapter(getActivity()
            .getApplicationContext(), R.layout.list_item_account, null,
            new String[] { DatabaseHelper.KEY_NAME },
            new int[] { R.id.account_name }, 0);

    setListAdapter(mCursorAdapter); 
    getLoaderManager().initLoader(0, null, this);
}

Later on, the ListFragment is replaced with another Fragment B. When the user presses the back button, Fragment B is removed and the ListFragment is added again. However, the list is empty and only the android:empty elements are displayed and none of the LoaderCallback methods are called. I can use the debugger to determine that getLoaderManager().initLoader(0, null, this); is actually called, but nothing else. When I change it to getLoaderManager().restartLoader(0, null, this);, the callbacks get called, but still my list remains empty (although there is data, the view is not refreshed).

How can I get my ListFragment to refresh itself when it is returned to the layout? Has anyone encountered this before, how did you fix it?

FYI, here are my callback methods

    @Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    return new AccountsCursorLoader(this.getActivity()
            .getApplicationContext());
}

@Override
public void onLoadFinished(Loader<Cursor> loaderCursor, Cursor cursor) {
    mCursorAdapter.swapCursor(cursor);
    mCursorAdapter.notifyDataSetChanged();
}

@Override
public void onLoaderReset(Loader<Cursor> arg0) {
    mCursorAdapter.swapCursor(null);
}

Some notes:

  1. I cannot use the setListShown(true) methods in the example because I get an IllegalStateException that it cannot be used with a custom content view.
  2. My AccountsCursorAdapter extends a SimpleCursorAdapter and modifies only the bindView() method.

Answer

Eureka!!! I found it (by accident, of course)

TL;DR;

In the AccountsListFragment, I now create my database adapter (AccountsDatabaseAdapter) in the onCreate() method and close it in the onDestroy() method and it now works. Previously I was creating my adapter in the onActivityCreated() and closing in onDestroyView(). Note that I am not referring to the ListAdapter, but rather to my database interfacing AccountsDbAdapter.

Attempt at long explanation:

Well, I was doing this because I thought that getting context through getActivity() will not be possible in the onCreate() method. It turns out you can getActivity() even before onActivityCreated() is called.

But I cannot really explain why this now works because the Loader has its own DatabaseAdapter object which it uses to retrieve the data. If I were to guess, I would say that the same database object is returned for both database adapters (the database is cached). Which would mean that when I close one in onDestroyView(), the other is also closed and the cursor data set becomes invalid which results in an empty list view. The loader does not reload because it thinks the data has not changed.

But even this explanation does not satisfy me completely, because some of the suggested solutions here which I tried like force restarting the loader each time did not work. (in the loadInBackground() method, a new DatabaseAdapter is created each time).

Anyway, if you happen to have a better understanding of what is going on, please hammer away in the comments.

Thanks everyone for the help with this!

Categories
discuss

How do I force JGroups which node to make coordinator?

I’m looking for a way to force JGroups to use a specific server as the Coordinator, and if that server isn’t present, elect a new Coordinator until that one specified rejoins the cluster and it takes over being the Coordinator.

In this case, we have some information we push in to the cluster by the Coordinator listening to a Topic for updates, however fetching & processing those updates can be resource intensive so we don’t want it to server anything to the outside world. So in the load-balancer in front of the cluster, we have it set to not send to the coordinator. But because the Coordinator is elected randomly, we basically need to shut down the cluster until only the single machine is in there and then start the rest of the cluster back up.

Answer

Currently there is no way to do this. Jgroups has spent considerable time making sure that the coordinator can be any of the nodes in a group. All tasks that maintain and monitor the health of the group membership list are shared among all of the members in the group to make sure that the coordinator duties do not affect the performance of the coordinator too much. The standard GMS (Group MembershipService) protocol stack class is what is responsible for the coordinator selection. Currently it is just the first host in the view list.

To get this behavior, you are going to have to implement your own protocol stack. Interestingly, I’ve been working on a protocol stack for Jgroups which implements approximately what you are asking for however it is not ready for prime time.

Others may have taken a whack at this issue however. I would recommend posting on the jgroups mailing list and asking the same question.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..