Java tutorial
/* * JBoss Admin * Copyright 2012, Christos Vasilakis, and individual contributors. * See the copyright.txt file in the distribution for a full * listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.cvasilak.jboss.mobile.admin.fragments; import android.app.AlertDialog; import android.app.ProgressDialog; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import com.actionbarsherlock.app.SherlockListFragment; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuItem; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import org.cvasilak.jboss.mobile.admin.JBossAdminApplication; import org.cvasilak.jboss.mobile.admin.R; import org.cvasilak.jboss.mobile.admin.model.Metric; import org.cvasilak.jboss.mobile.admin.net.Callback; import org.cvasilak.jboss.mobile.admin.net.JBossOperationsManager.JMSType; import org.cvasilak.jboss.mobile.admin.util.MetricsAdapter; import org.cvasilak.jboss.mobile.admin.util.commonsware.MergeAdapter; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; public class JMSTopicMetricsViewFragment extends SherlockListFragment { private static final String TAG = JMSTopicMetricsViewFragment.class.getSimpleName(); private JBossAdminApplication application; private ProgressDialog progress; ArrayList<Metric> inFlightMetrics; ArrayList<Metric> msgProcessedMetrics; ArrayList<Metric> subscriptionMetrics; private String topicName; public static JMSTopicMetricsViewFragment newInstance(String name) { JMSTopicMetricsViewFragment f = new JMSTopicMetricsViewFragment(); Bundle args = new Bundle(); args.putString("topicName", name); f.setArguments(args); return f; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "@onCreate()"); this.topicName = getArguments().getString("topicName"); application = (JBossAdminApplication) getActivity().getApplication(); // restore state in case of a configuration change (eg orientation) if (savedInstanceState != null) { inFlightMetrics = savedInstanceState.getParcelableArrayList("inFlightMetrics"); msgProcessedMetrics = savedInstanceState.getParcelableArrayList("msgProcessedMetrics"); subscriptionMetrics = savedInstanceState.getParcelableArrayList("subscriptionMetrics"); } MergeAdapter adapter = new MergeAdapter(); TextView sectionHeader; // Section: In-Flight Messages sectionHeader = new TextView(getActivity()); sectionHeader.setBackgroundColor(Color.DKGRAY); sectionHeader.setPadding(15, 10, 0, 10); sectionHeader.setText("In-Flight Messages"); adapter.addView(sectionHeader); if (inFlightMetrics == null) { inFlightMetrics = new ArrayList<Metric>(); inFlightMetrics.add(new Metric("Messages In Topic", "message-count")); inFlightMetrics.add(new Metric("In Delivery", "delivering-count")); } MetricsAdapter inFlightMetricsAdapter = new MetricsAdapter(getSherlockActivity(), inFlightMetrics); adapter.addAdapter(inFlightMetricsAdapter); // Section: Messages Processed sectionHeader = new TextView(getActivity()); sectionHeader.setBackgroundColor(Color.DKGRAY); sectionHeader.setPadding(15, 10, 0, 10); sectionHeader.setText("Messages Processed"); adapter.addView(sectionHeader); if (msgProcessedMetrics == null) { msgProcessedMetrics = new ArrayList<Metric>(); msgProcessedMetrics.add(new Metric("Messages Added", "messages-added")); msgProcessedMetrics.add(new Metric("Durable Messages", "durable-message-count")); msgProcessedMetrics.add(new Metric("NoN-Durable Messages", "non-durable-message-count")); } MetricsAdapter msgProcessedMetricsAdapter = new MetricsAdapter(getSherlockActivity(), msgProcessedMetrics); adapter.addAdapter(msgProcessedMetricsAdapter); // Section: Consumer sectionHeader = new TextView(getActivity()); sectionHeader.setBackgroundColor(Color.DKGRAY); sectionHeader.setPadding(15, 10, 0, 10); sectionHeader.setText("Subscriptions"); adapter.addView(sectionHeader); if (subscriptionMetrics == null) { subscriptionMetrics = new ArrayList<Metric>(); subscriptionMetrics.add(new Metric("Number of Subscriptions", "subscription-count")); subscriptionMetrics.add(new Metric("Durable Subscribers", "durable-subscription-count")); subscriptionMetrics.add(new Metric("Non-Durable Subscribers", "non-durable-subscription-count")); } MetricsAdapter subscriptionerMetricsAdapter = new MetricsAdapter(getSherlockActivity(), subscriptionMetrics); adapter.addAdapter(subscriptionerMetricsAdapter); setListAdapter(adapter); // inform runtime that we have an action button (refresh) setHasOptionsMenu(true); // refresh only if the fragment is created // for the first time (no previous state) if (savedInstanceState == null) refresh(); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putParcelableArrayList("inFlightMetrics", inFlightMetrics); outState.putParcelableArrayList("msgProcessedMetrics", msgProcessedMetrics); outState.putParcelableArrayList("subscriptionMetrics", subscriptionMetrics); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_refresh, menu); super.onCreateOptionsMenu(menu, inflater); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.refresh) { refresh(); return (true); } return (super.onOptionsItemSelected(item)); } public void refresh() { progress = ProgressDialog.show(getSherlockActivity(), "", getString(R.string.queryingServer)); application.getOperationsManager().fetchJMSQueueMetrics(topicName, JMSType.TOPIC, new Callback() { @Override public void onSuccess(JsonElement reply) { progress.dismiss(); JsonObject jsonObj = reply.getAsJsonObject(); Map<String, String> info = new HashMap<String, String>(); // common metrics int msgCount = jsonObj.getAsJsonPrimitive("message-count").getAsInt(); int delivCount = jsonObj.getAsJsonPrimitive("delivering-count").getAsInt(); float delivPerc = (msgCount != 0 ? ((float) delivCount / msgCount) * 100 : 0); int msgAdded = jsonObj.getAsJsonPrimitive("messages-added").getAsInt(); info.put("message-count", String.format("%d", msgCount)); info.put("delivering-count", String.format("%d (%.0f%%)", delivCount, delivPerc)); info.put("messages-added", String.format("%d", msgAdded)); for (Metric metric : inFlightMetrics) { metric.setValue(info.get(metric.getKey())); } int durCount = jsonObj.getAsJsonPrimitive("durable-message-count").getAsInt(); float durPerc = (msgAdded != 0 ? ((float) durCount / msgAdded) * 100 : 0); int nonDurCount = jsonObj.getAsJsonPrimitive("non-durable-message-count").getAsInt(); float nonDurPerc = (msgAdded != 0 ? ((float) nonDurCount / msgAdded) * 100 : 0); int subCount = jsonObj.getAsJsonPrimitive("subscription-count").getAsInt(); int durSubCount = jsonObj.getAsJsonPrimitive("durable-subscription-count").getAsInt(); int nonDurSubCount = jsonObj.getAsJsonPrimitive("non-durable-subscription-count").getAsInt(); info.put("durable-message-count", String.format("%d (%.0f%%)", durCount, durPerc)); info.put("non-durable-message-count", String.format("%d (%.0f%%)", nonDurCount, nonDurPerc)); info.put("subscription-count", String.format("%d", subCount)); info.put("durable-subscription-count", String.format("%d", durSubCount)); info.put("non-durable-subscription-count", String.format("%d", nonDurSubCount)); for (Metric metric : msgProcessedMetrics) { metric.setValue(info.get(metric.getKey())); } for (Metric metric : subscriptionMetrics) { metric.setValue(info.get(metric.getKey())); } // refresh table ((MergeAdapter) getListAdapter()).notifyDataSetChanged(); } @Override public void onFailure(Exception e) { progress.dismiss(); AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity()); alertDialog.setTitle(R.string.dialog_error_title).setMessage(e.getMessage()) .setPositiveButton(R.string.dialog_button_Bummer, null).setCancelable(false) .setIcon(android.R.drawable.ic_dialog_alert).show(); } }); } }