private void renderApplianceConfiguration(
      String appRoot, IApplianceConfiguration config, Integer endPointId, PrintWriter pw) {
    if (config == null) return;
    String appliancePid = config.getAppliancePid();
    IAppliance appliance = appliancesProxy.getAppliance(appliancePid);
    if (appliance == null) appliance = appliancesProxy.getInstallingAppliance(appliancePid);

    String name = config.getName(null);
    ICategory category = appliancesProxy.getCategory(config.getCategoryPid(null));
    ILocation location = appliancesProxy.getLocation(config.getLocationPid(null));
    pw.println(
        (appliance.isDriver() ? "<b><u><br/>DRIVER" : "<b><u><br/>VIRTUAL")
            + " APPLIANCE</u>"
            + " (<a href=\""
            + appRoot
            + "/"
            + LABEL
            + "/config/"
            + appliancePid
            + (endPointId != null ? ("/" + endPointId) : "")
            + "\">Reload page</a>"
            + "&nbsp;&nbsp;&nbsp;<a href=\""
            + appRoot
            + "/"
            + LABEL
            + "/"
            + appliancePid
            + "\">Go to appliance details</a>)</br>"
            + "<br/>PID: "
            + appliance.getPid()
            + "<br/>TYPE: "
            + appliance.getDescriptor().getType()
            + ((name != null) ? "<br/>Name: " + name : "")
            + ((category != null) ? "<br/>Category: " + category.getName() : "")
            + ((location != null) ? "<br/>Location: " + location.getName() : "")
            + "</b><br/><hr/>");

    pw.println(
        "<br/><form name=\"ApplianceConfig\""
            + " action=\""
            + appRoot
            + "/"
            + LABEL
            + "/config/"
            + appliancePid
            + (endPointId != null ? ("/" + endPointId) : "")
            + "\" method=\"get\">");
    pw.println("<table id=\"ApplianceConfig\" class=\"nicetable\"><tbody>");
    pw.println("<tr><td width=\"50%\">");
    if (endPointId != null) {
      IEndPoint endPoint = appliance.getEndPoint(endPointId.intValue());
      pw.println(
          "<b>End point configuration (ID: "
              + endPointId
              + ", TYPE: "
              + endPoint.getType()
              + ")</b><br/>");
    } else {
      pw.println("<b>Appliance configuration</b><br/>");
    }
    pw.println("</b></td><td><input type=\"submit\" value=\"update\"/></td></tr>");

    name = config.getName(endPointId);
    String categoryPid = config.getCategoryPid(endPointId);
    String locationPid = config.getLocationPid(endPointId);
    pw.println(
        "<tr><td width=\"50%\">Name: </td><td><input type=\"text\" name=\"param\" size=\"30\" value=\""
            + name
            + "\"/></td></tr>");

    pw.println("</td></tr>");
    String pid = null;
    ICategory[] categories = appliancesProxy.getCategories();
    if (categories != null && categories.length > 0) {
      pw.println("<tr><td width=\"50%\">Category: </td><td><select name=\"param\">");
      for (int i = 0; i < categories.length; i++) {
        pid = categories[i].getPid();
        pw.println(
            "<option value=\""
                + pid
                + (pid.equals(categoryPid) ? "\" selected=\"selected\">" : "\" >")
                + categories[i].getName()
                + "</option>");
      }
      pw.println("</td></tr>");
    }

    ILocation[] locations = appliancesProxy.getLocations();
    if (locations != null && locations.length > 0) {
      pw.println("<tr><td width=\"50%\">Location: </td><td><select name=\"param\">");
      pid = null;
      for (int i = 0; i < locations.length; i++) {
        pid = locations[i].getPid();
        pw.println(
            "<option value=\""
                + pid
                + (pid.equals(locationPid) ? "\" selected=\"selected\">" : "\" >")
                + locations[i].getName()
                + "</option>");
      }
      pw.println("</td></tr>");
    }
    pw.println("</tbody></table></form>");
  }
  protected void renderContent(HttpServletRequest req, HttpServletResponse response)
      throws ServletException, IOException {
    PrintWriter pw = response.getWriter();
    if (appliancesProxy == null) return;

    String appRoot = req.getContextPath() + req.getServletPath();
    String pathInfo = req.getPathInfo();
    pathInfo = pathInfo.substring(LABEL.length() + 1);
    StringTokenizer st = null;
    String appliancePid;
    try {
      if (pathInfo.startsWith("/confirm")) {
        st = new StringTokenizer(pathInfo, "/");
        if (st.hasMoreTokens()) st.nextToken();
        String confirmParam = null;
        if (st.hasMoreTokens()) confirmParam = st.nextToken();
        appliancePid = null;
        if (st.hasMoreTokens()) appliancePid = st.nextToken();
        if (confirmParam.equals("delete"))
          pw.println(
              "</br></br><b><font color=\"red\">Are you sure you want to delete appliance "
                  + appliancePid
                  + "?&nbsp;&nbsp;&nbsp;<a href=\""
                  + appRoot
                  + "/"
                  + LABEL
                  + "/delete/"
                  + appliancePid
                  + "\">yes</a>&nbsp;&nbsp;&nbsp;"
                  + "<a href=\""
                  + appRoot
                  + "/"
                  + LABEL
                  + "\">no</a>");
      } else if (pathInfo == null
          || pathInfo.equals("")
          || pathInfo.equals("/")
          || pathInfo.startsWith("/install")
          || pathInfo.startsWith("/delete")) {
        List v = null;
        boolean installing = false;
        boolean deleteStatus = true;
        boolean closeStatus = true;
        boolean openStatus = true;
        boolean installStatus = true;
        if (pathInfo.startsWith("/delete")) {
          st = new StringTokenizer(pathInfo, "/");
          if (st.hasMoreTokens()) st.nextToken();
          appliancePid = null;
          if (st.hasMoreTokens()) appliancePid = st.nextToken();
          deleteStatus = appliancesProxy.deleteAppliance(appliancePid);
        }
        if (pathInfo.startsWith("/install")) {
          installing = true;
          String installParam = null;
          st = new StringTokenizer(pathInfo, "/");
          if (st.hasMoreTokens()) st.nextToken();
          if (st.hasMoreTokens()) installParam = st.nextToken();
          if (installParam != null) {
            if (installParam.equals("open")) openStatus = openZigBeeNetwork();
            else if (installParam.equals("close")) closeStatus = closeZigBeeNetwork();
            else installStatus = appliancesProxy.installAppliance(installParam);
          }
          v = appliancesProxy.getInstallingAppliancePids();
          pw.println(
              "<br/><b><u>INSTALLING APPLIANCES</u>&nbsp;&nbsp;&nbsp;(<a href=\""
                  + appRoot
                  + "/"
                  + LABEL
                  + "/install\">Reload page</a>&nbsp;&nbsp;&nbsp;<a href=\""
                  + appRoot
                  + "/"
                  + LABEL
                  + "\">Go to installed appliances</a>)</b></br>");
          pw.println(
              "<br/><b>[<a href=\""
                  + appRoot
                  + "/"
                  + LABEL
                  + "/install/open\">Open network</a>&nbsp;&nbsp;&nbsp;<a href=\""
                  + appRoot
                  + "/"
                  + LABEL
                  + "/install/close\">Close network</a>]</b>");
          if (!openStatus)
            pw.println(
                "<b><font color=\"red\">Some problem occurred while opening network</font></b>");
          if (!closeStatus)
            pw.println(
                "<b><font color=\"red\">Some problem occurred while closing network</font></b>");
          if (!installStatus)
            pw.println(
                "<b><font color=\"red\">Some problem occurred while installing appliance "
                    + installParam
                    + "</font></b>");
          pw.println("</br><hr/>");
        } else {
          v = appliancesProxy.getAppliancePids();
          v.add(PROXY_APPLIANCE_PID);
          pw.println(
              "<br/><b><u>INSTALLED APPLIANCES</u>&nbsp;&nbsp;&nbsp;(<a href=\""
                  + appRoot
                  + "/"
                  + LABEL
                  + "\">Reload page</a>&nbsp;&nbsp;&nbsp;<a href=\""
                  + appRoot
                  + "/"
                  + LABEL
                  + "/install\">Go to installing appliances</a>)</b></br><hr/>");
        }
        List driverApplianceList = new ArrayList();
        List virtualApplianceList = new ArrayList();
        List singletonApplianceList = new ArrayList();
        if (v != null) {
          appliancePid = null;
          IAppliance appliance = null;
          for (Iterator iterator = v.iterator(); iterator.hasNext(); ) {
            appliancePid = (String) iterator.next();
            if (installing) appliance = appliancesProxy.getInstallingAppliance(appliancePid);
            else appliance = appliancesProxy.getAppliance(appliancePid);
            if (appliance != null) {
              if (appliance.isSingleton()) singletonApplianceList.add(appliance);
              else if (appliance.isDriver()) driverApplianceList.add(appliance);
              else virtualApplianceList.add(appliance);
            }
          }
          for (Iterator iterator = driverApplianceList.iterator(); iterator.hasNext(); ) {
            appliance = (IAppliance) iterator.next();
            renderAppliance(appRoot, installing, false, appliance, pw);
          }
          pw.println("<hr/>");
          for (Iterator iterator = virtualApplianceList.iterator(); iterator.hasNext(); ) {
            appliance = (IAppliance) iterator.next();
            renderAppliance(appRoot, installing, false, appliance, pw);
          }
          pw.println("<hr/>");
          for (Iterator iterator = singletonApplianceList.iterator(); iterator.hasNext(); ) {
            appliance = (IAppliance) iterator.next();
            renderAppliance(appRoot, installing, false, appliance, pw);
          }
        }
      } else if (pathInfo.startsWith("/config")) {
        st = new StringTokenizer(pathInfo, "/");
        st.nextToken();
        appliancePid = st.nextToken();
        Integer endPointId = null;
        if (st.hasMoreElements()) {
          endPointId = new Integer(st.nextToken());
        }
        String[] params = req.getParameterValues("param");
        IApplianceConfiguration config = appliancesProxy.getApplianceConfiguration(appliancePid);
        if (config != null && params != null && params.length >= 1) {
          config.updateName(endPointId, params[0]);
          config.updateCategoryPid(endPointId, params[1]);
          config.updateLocationPid(endPointId, params[2]);
          appliancesProxy.updateApplianceConfiguration(config);
        }
        renderApplianceConfiguration(
            appRoot, appliancesProxy.getApplianceConfiguration(appliancePid), endPointId, pw);
      } else {
        st = new StringTokenizer(pathInfo, "/");
        int i = 0;
        appliancePid = null;
        int endPointId = -1;
        String clusterName = null;
        String methodName = null;
        while (st.hasMoreElements()) {
          switch (i) {
            case 0:
              appliancePid = st.nextToken();
              break;
            case 1:
              endPointId = Integer.parseInt(st.nextToken());
              break;
            case 2:
              clusterName = st.nextToken();
              break;
            case 3:
              methodName = st.nextToken();
              break;
            default:
              break;
          }
          i++;
        }
        String[] params = req.getParameterValues("param");
        IAppliance appliance = appliancesProxy.getAppliance(appliancePid);
        if (appliance == null) appliance = appliancesProxy.getInstallingAppliance(appliancePid);
        if (appliance == null) {
          pw.println(
              "<br><br>Please wait some seconds and try to reload this web page or click on the AH tab...");
          return;
        }
        IApplianceConfiguration config = appliancesProxy.getApplianceConfiguration(appliancePid);
        if (endPointId < 0) {
          renderAppliance(appRoot, false, true, appliance, pw);
        } else {
          renderClusterCommands(
              appRoot,
              config,
              appliance.getEndPoint(endPointId),
              clusterName,
              methodName,
              params,
              pw);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }