<?php
/**
 * Configuration information
 *
 * @author Sam
 */

require_once "WEB-INF/php/inc.php";

if (! admin_init()) {
  return;
}

echo "<h1>" . gettext('Configuration') . "</h1>";

echo "<div id='config-tabs'>";
echo "<ul>";
$count = 0;
$sections = array('environment', 'server', 'webapps', 'database');

foreach ($sections as $name) {
  $count++;

  echo "<li><a href='#$name'>" . gettext($name) . "</a></li>";
}
echo "</ul>\n";

?>

<div id='environment'>
<h2><?= gettext('Server')?>: <?= $g_server->Id ? $g_server->Id : "default" ?></h2>
<div class='section'>
<table class="data" width='100%'>
  <tr title="<?= gettext('The server id used when starting this instance of Resin, the value of \'-server\'.')?>">
    <th width="25%" scope="row"><?= gettext('Server id:')?></th>
    <td width="75%"><?= $g_server->Id ? $g_server->Id : '""' ?></td>
  </tr>

  <tr title="<?= gettext('The configuration file used when starting this instance of Resin, the value of \'-conf\'.')?>">
    <th width="25%" scope="row"><?= gettext('Version:')?></th>
    <td width="75%"><?= $g_resin->Version ?></td>
  </tr>

  <tr title="<?= gettext('The configuration file used when starting this instance of Resin, the value of \'-conf\'.')?>">
    <th width="25%" scope="row"><?= gettext('Config file:')?></th>
    <td width="75%"><?= $g_resin->ConfigFile ?></td>
  </tr>

  <tr title="<?= gettext('The Resin home directory used when starting this instance of Resin. This is the location of the Resin program files.')?>">
   <th width="25%" scope="row"><?= gettext('Resin home:')?></th>
    <td width="75%"><?= $g_resin->ResinHome ?></td>
  </tr>

  <tr title="<?= gettext('The resin root directory used when starting this instance of Resin. This is the root directory of the web server files.')?>">
    <th width="25%" scope="row"><?= gettext('Resin root:')?></th>
    <td width="75%"><?= $g_resin->RootDirectory ?></td>
  </tr>
  </table>
<?php

  $env = $g_server->Environment;
   
  echo "<h3>";
  echo "<span id='sw_classpath' class='switch'></span> ";
  echo "Classpath"
  echo "</h3>\n";
  
  echo "<pre class='toggle-sw_classpath'>";
  foreach ($env->ClassPath as $item) {
    echo $item . "\n";
  }
  echo "</pre>";
  
?>
</div> <!-- section -->
</div> <!-- environment -->

<!--
   - XML Server configuration
  -->
  
<div id='server'>
<h2><?= gettext('Server Configuration')?></h2>
<div class="section">
<!-- XML config -->
<?php
  $resin_mbean = $g_mbean_server->lookup("resin:type=Resin");
  display_config($resin_mbean);
?>
</div> <!-- section -->
</div> <!-- server -->

<!--
   - webapps configuration
  -->

<div id='webapps'>

<?php
$cluster = $g_server->Cluster;
$cluster_name = empty($cluster->Name) ? "default" : $cluster->Name;

echo "<h1>" . gettext('Cluster:') . " $cluster_name</h1>";
echo "<div class='section'>\n";

$servers = $cluster->Servers;

?>
<!-- host data -->

<?php

$hosts = $g_mbean_server->query("resin:*,type=Host");

usort($hosts, "sort_host");

foreach ($hosts as $host) {
  $hostName = empty($host->HostName) ? "default" : $host->HostName;
?>

  <h2><?= gettext('Host')?> <?= $host->URL ?></h2>
  <div class='section'>

  <table class='data' width='100%'>
  <tr>
    <th width="25%" class='item' scope="row">root-directory</th>
    <td width="75%"><?= $host->RootDirectory ?></td>
  </tr>
  </table>

<?php

echo "<h3>" . gettext('WebApps') . "</h3>\n";
echo "<div class='section'>\n";

$webapps = $host->WebApps;

usort($webapps, "sort_webapp");
$count = 0;
foreach ($webapps as $webapp) {
  $session = $webapp->SessionManager;
  $persistent_store = $session->PersistentStore;
?>

  <h3><span id='sw_webapp_<?= ${count} ?>' class='switch'> </span><?= $webapp->Name ?></h3>
  <div class="toggle-sw_webapp_<?= ${count} ?>">
  <table class='data' width='100%'>
  <tr>
  	<th width="25%" class='group' scope='row'>context-path</th>
  	<td width="75%">
       <?= empty($webapp->ContextPath) ? "/" : $webapp->ContextPath ?>
    </td>
  </tr>
  <tr>
    <th width="25%" class='item' width='25%' scope="row">root-directory</th>
    <td width="75%">
       <?= $webapp->RootDirectory ?>
    </td>
  </tr>

  <tr>
    <th width="25%" class='item' scope="row">session-timeout</th>
    <td width="75%">
       <?= $session->SessionTimeout / 1000 ?>s
    </td>
  </tr>

  <tr>
    <th width="25%" class='item' scope="row">session-max</th>
    <td width="75%">
       <?= $session->SessionMax ?>
    </td>
  </tr>

<?php if ($persistent_store) { ?>

  <tr>
    <th width="25%" class='item' scope="row">persistent-store</th>
    <td width="75%">
       <?= $persistent_store->StoreType ?>
    </td>
  </tr>

  <tr>
    <th width="25%" class='item' scope="row">save-mode</th>
    <td width="75%">
       <?= $session->SaveMode ?>
    </td>
  </tr>

<?php } // persistent
    echo "</table>";
  
    if ($webapp->Configs) {
      echo "<h4>WebApp Configuration</h4>\n";
      echo "<div class='section'>\n";
      display_config($webapp);
      echo "</div>";
    }
    
    echo "</div>\n"; // toggle-sw_webapp    
  
    $count++;
    
  } // webapps
  
  echo "</div>"; // section under webapps
  echo "</div>"; // section under host url
 } // hosts
 
 echo "</div>"; // section under cluster

?>
</div> <!-- webapps -->

<!--
   - database configuration
  -->

<div id='database'>

<?php

$databases = $g_mbean_server->query("resin:type=Database,*");

echo "<h2>Database Configuration</h2>\n"
echo "<div class='section'>\n";

$count = 0;
foreach ($databases as $db) {
  $count++;
  
  echo "<h3>";
  echo "<span id='sw_database_${count}' class='switch'></span> ";
  echo jmx_short_name($db->mbean_name, array("type"));
  echo "</h3>\n";

  echo "<pre class='toggle-sw_database_${count}'>";
  print_database_config($db);
  echo "</pre>";
}

echo "</div>";
echo "</div>";

function display_config($parent_mbean)
{
  $configs = $parent_mbean->Configs;
  
  $count = 0;
  foreach ($configs as $config) {
    $count++;
    
    $path = $config->Path;
    $hash = $config->hashCode();
    
    echo "<h3><span id='sw_$hash' class='switch'></span> ";
    echo "$path"
    echo "</h3>\n";
  
    echo "<div class='section toggle-sw_$hash'>";
    ?>
    
    <div class="section">
      <table class="data" width='100%'>
        <tr title="<?= gettext('The length of the file in bytes.')?>">
          <th width="25%" scope="row"><?= gettext('Length')?></th>
          <td width="75%"><?= $config->Length > 0 ? $config->Length : "" ?></td>
        </tr>
        <tr title="<?= gettext('The last modified date of the file.')?>">
          <th width="25%" scope="row"><?= gettext('Last Modified')?></th>
          <td width="75%"><?= $config->LastModified> 0 ? date("Y-m-d H:i:s", $config->LastModified/1000) : gettext('Unavailable') ?></td>
        </tr>
        <tr title="<?= gettext('The crc of the file.')?>">
          <th width="25%" scope="row"><?= gettext('CRC-64')?></th>
          <td width="75%"><?= $config->Crc64 != 0 ? $config->Crc64 : gettext('Unavailable') ?></td>
        </tr>
      </table>
		</div>
		  
    <?php
   
	  echo "</div>";
  }
  
}

function print_database_config($db)
{
  echo "&lt;database name=\"" . $db->Name . "\">\n";

  foreach ($db->Drivers as $driver) {
    echo "  &lt;driver";
    echo " url=\"";
    echo $driver->getUrl();
    echo "\"\n";
    echo "          class=\"";
    echo $driver->getClassName();
    echo "\">\n";

    foreach ($driver->Properties as $key => $value) {
      echo "    &lt;" . $key . ">" . $value . "&lt;/" . $key . "/>\n";
    }

    echo "  &lt;/driver>\n\n";
  }

  echo "  &lt;spy>";
  echo $db->Spy ? "true" : "false";
  echo "&lt;/spy>\n\n";

  echo "  &lt;max-connections>";
  echo $db->MaxConnections;
  echo "&lt;/max-connections>\n";

  echo "  &lt;max-idle-count>";
  echo $db->MaxIdleCount;
  echo "&lt;/max-idle-count>\n";

  echo "  &lt;max-create-connections>";
  echo $db->MaxCreateConnections;
  echo "&lt;/max-create-connections>\n";

  echo "  &lt;max-overflow-connections>";
  echo $db->MaxOverflowConnections;
  echo "&lt;/max-overflow-connections>\n";

  echo "\n";

  echo "  &lt;max-idle-time>";
  echo display_timeout($db->MaxIdleTime);
  echo "&lt;/max-idle-time>\n";

  echo "  &lt;max-active-time>";
  echo display_timeout($db->MaxActiveTime);
  echo "&lt;/max-active-time>\n";

  echo "  &lt;max-pool-time>";
  echo display_timeout($db->MaxPoolTime);
  echo "&lt;/max-pool-time>\n";

  echo "  &lt;connection-wait-time>";
  echo display_timeout($db->ConnectionWaitTime);
  echo "&lt;/connection-wait-time>\n";

  echo "&lt;/database>\n";
}

display_footer($g_page, javascript_create_tab("config-tabs"));

?>
